0

I would like to generate an Excel (or CSV) file to have a list of all users and what type of access they have to a certain 2010 site? This site is secure, but I do have administrative permissions to it.

I have tried a couple PowerShell scripts on it, but they generate blank .csv files. Am I doing something wrong with them?

Any other suggestions?

1 Answer 1

0

The following JSOM code worked for me to get all the users of a 2013 site, not sure about 2010. Openn the site in chrome. Copy the whole code and run in console. Doesn't work in IE.

var JSONToCSVConvertor = function (JSONData, ReportTitle, ShowLabel) {
        var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

        var CSV = '';
        //Set Report title in first row or line
        //CSV += ReportTitle + '\r\n\n';
        //This condition will generate the Label/Header
        if (ShowLabel) {
            var row = "";
            //This loop will extract the label from 1st index of on array
            for (var index in arrData[0]) {
                //Now convert each value to string and comma-seprated
                row += index + ',';
            }

            row = row.slice(0, -1);

            //append Label row with line break
            CSV += row + '\r\n';
        }

        //1st loop is to extract each row
        for (var i = 0; i < arrData.length; i++) {
            var row = "";
            for (var index in arrData[i]) {
                row += '"' + arrData[i][index] + '",';
            }
            row = row.slice(0, row.length - 1);

            CSV += row + '\r\n';
        }

        if (CSV == '') {
            alert("Invalid data");
            return;
        }

        //Generate a file name
        var fileName = "MyReport_";
        //this will remove the blank-spaces from the title and replace it with an underscore
        fileName += ReportTitle.replace(/ /g, "_");

        //Initialize file format you want csv or xls
        var uri = CSV;
        return uri;
    }

    var cleanUp = function (a) {
        a.textContent = 'Downloaded';
        a.dataset.disabled = true;

        // Need a small delay for the revokeObjectURL to work properly.
        setTimeout(function () {
            window.URL.revokeObjectURL(a.href);
        }, 1500);
    };


    var exportToCSV = function (resultRows) {
        var csvstring = JSONToCSVConvertor(resultRows, "", true)
        var testanc = document.createElement('a');
        if (testanc.download !== undefined) {
            var link = document.createElement('a');
            var mimeType = 'text/plain';
            var blob = new Blob([csvstring], {
                type: mimeType
            });
            var url = URL.createObjectURL(blob);
            link.href = url;
            link.download = _spPageContextInfo.webTitle+'_UsersInfo.csv';
            link.dataset.downloadurl = [mimeType, link.download, link.href].join(':');
            link.innerHTML = "Export to CSV";
            document.body.appendChild(link);
            link.onclick = function (e) {
                if ('disabled' in this.dataset) {
                    return false;
                }
                cleanUp(this);
            };

            link.click();
            document.body.removeChild(link);
        }

        else {

        }
    };

    var dlg = {};
    if(window.SP&&SP.UI&&SP.UI.ModalDialog&&SP.UI.ModalDialog.showWaitScreenWithNoClose){
        dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose('Downloading file','Please wait')
    }
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var users = web.get_siteUsers();
        context.load(users);
       var csvsstring = '';
         var usersarray = [];
        context.executeQueryAsync(function() {
            var enumer = users.getEnumerator();
            while (enumer.moveNext()) {
                var currentUser = enumer.get_current();
              //  console.log(currentUser.get_title());
               usersarray.push({
        Id:currentUser.get_id(),
        Title:currentUser.get_title(),
        Email:currentUser.get_email(),
        LoginName:currentUser.get_loginName()
               })
            }
    exportToCSV(usersarray);
    if(dlg.close){
        dlg.close()
    }
        }, function(s, a) {
            alert(a.get_message())
        });
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.