Pages

Monday, September 28, 2015

Get SharePoint Site Users and User Profiles using JSOM


Using SharePoint JavaScript Client Side Object Model framework, we can get all site users information and each user profile properites (ex. Birthday, Work Email, Phone...etc).

1. To get site users information, use SP.Web.siteUsers property
2. then use SP.UserProfiles.PeopleManager.getUserProfilePropertyFor Method to get user profile properties.

Example code:

//Usage
var scriptbase = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.js', function () {
  $.getScript(scriptbase + 'SP.UserProfiles.js', function () {
    getUsersBirthdays(function(usersProperties){
       for(var i = 0; i < usersProperties.length;i++)
       {
           console.log(usersProperties[i].get_value());
       }
    },
    function(sender,args){
       console.log(args.get_message());
    });
  });
});

//Get Birthday User Profile Property for Site Users
function getUsersBirthdays(Success,Error) {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();

    var users = web.get_siteUsers();
    clientContext.load(users);
    clientContext.executeQueryAsync(
    function() {
       var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
       var personsProperties = [];
       for(var i = 0; i < users.get_count();i++)
       {
           var user = users.getItemAtIndex(i);
           var personBirthday = peopleManager.getUserProfilePropertyFor(user.get_loginName(),'SPS-Birthday');
           personsProperties.push(personBirthday);
       }

       clientContext.executeQueryAsync(
           function() {
             Success(personsProperties);
           },
           Error);

    },
    Error);
}



Create SharePoint Site Collection with site template


The great feature in SharePoint site that you can take the complete site with content as template and can re-use it else where to create a site based out of it. Basically, will upload site templates and create sub sites based on custom site template.

In this article, will show how to create a whole Site Collection based on site template. Create the site template as you normally would be going into Site Settings for that site and then selecting Save site as a template.










































Site template will be saved in 'Solutions' gallery of SharePoint site collection. Go and download it your local and keep it to upload later.

Here's the steps,
Go to SharePoint Online administration and click on 'Private Site Collection' and will get below window to provide new site collection details,













































Along with all other details, in 'Template Selection' section select preferred language and select 'Custom' tab as shown in above image. Select option '

After site collection created, open new site collection URL. Since the site doesn’t have a template yet you will be prompted to choose one. At the top you’ll see the standard SharePoint templates but below this you’ll see option to upload something to the Solution Gallery of the new site collection. You should select this.



















You should see that the Solution Gallery is empty. You now need to upload the template you downloaded from the previous site that was saved on your workstation. To do this simply select the Upload Solution button in the top left and proceed to upload the template from your workstation.





























Just before you close the dialog displayed after uploading the file completes you should select the Activate button on the right of the Ribbon Menu to make that template available. Failing to do this will mean that it can’t used.














You should now see a single activated solution in the gallery as shown above.






























You’ll need to browse to the URL of your new site collection again but now if you select the Custom tab in the Template Selection area you should see the name of your custom site template as shown above. Select this and press the OK button to proceed with the creation process.

The time it takes to create the new root site in the new site collection depends on how large your custom template is but in a short period of time the new site collection based on your custom template will be created. All you need to do is set the security groups and you are good to go.

That’s how you create a completely new site collection from an existing site template.

SharePoint Designer Online Server Error:'Access Denied' on modify anything


Most probably get 'Access Denied' when Custom Script feature is turned off. This will not allow to check-out/check-in or do save operation on anything in SharePoint site. Please follow the below steps to enable the Custom Script feature and that will solve the issue.

1. Sign in to Office 365 with your work or school account.
2. Go to the SharePoint admin center.
3. Select Settings.
4. Under Custom Script choose:

- Prevent users from running custom script on personal sites or Allow users to run custom script on personal sites.
- Prevent users from running custom script on user created sites or Allow users to run custom script on self-service created sites.







5. Select OK. It takes about 24 hours for the change to take effect 
Also, we can enable the feature using powershell script and that will take effect immediately. Execute below scripts in SharePoint Online Management Shell. 

Connect-SPOService -Url https://testspsite-admin.sharepoint.com -credential spadmin@testspsite.onmicrosoft.com

Set-SPOSite -Identity "https://testspsite.sharepoint.com" -DenyAddAndCustomizePages $false

After run above two powershell scripts, you can start modify files immediately. 

Hope this helps.