Sunday 3 July 2016

Working With SharePoint Web Scoped Features Using PnP PowerShell

In this post, you will learn, how we can retrieve, activate or deactivate Web scoped features on SharePoint site or sub sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite:

You need to have PowerShell 3.0 available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation on the official site. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).

The following operations will be compatible with SharePoint 2013 On Premise and Office 365 versions.

Connect To Site:

Connect to the site, using the snippet, given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).
  1. #Get Current Context Site (Root or subsite)  
  2. $siteurl = "https://abc.sharepoint.com/subsite"  
  3. Connect-SPOnline -Url $siteurl  
  4. $ctx = Get-SPOContext  
Since, we are working with the features with Web Scope, the site URL can be the site collection or sub sites available. Once connected, you can carry out any of the operations mentioned below, based on the requirement.

Retrieve Web Scope Features:

The Web scoped features active on the site or sub sites are retrieved in the operation, given below. Get-SPOFeature command is used to get all the features from the site. Since, we are retrieving the Web scoped features, the scope parameter is not required in this operation.

Each and every feature information is retrieved, using for each loop. The display name and definition Id are retrieved from the features. The code snippet given below helps retrieving all the Web scoped features from the site or sub site, specified in the context.
  1. function RetrieveFeatures(){  
  2.     # Get Web Scoped features  
  3.     $features = Get-SPOFeature -Scope Web  
  4.     Write-Host "Total active features count " $features.Count  
  5.     foreach($feature in $features){  
  6.         Write-Host "Feature Name : " $feature.DisplayName  
  7.         Write-Host "Feature ID   : " $feature.DefinitionId  
  8.     }  
  9.       
  10. }  
  11. RetrieveFeatures #Get all web scoped features from site   
The feature can be retrieved, using the identity. The identity can be the feature name or Id. The scope parameter is optional. Though, Web is passed as a scope. The following code snippet retrieves the feature, using the feature Id.
  1. function RetrieveFeature(){  
  2.     $features = Get-SPOFeature -Scope Web  
  3.     # Check whether FollowingContent feature is active  
  4.     $feature = Get-SPOFeature -Identity a7a2793e-67cd-4dc1-9fd0-43f61581207a -Scope Web  
  5.     if($feature.DisplayName -ne $null){  
  6.         Write-Host $feature.DisplayName " feature is active"          
  7.     }  
  8.     else{  
  9.         Write-Host "Couldn't find info about feature id. Feature is not active"  
  10.     }  
  11. }  
  12. RetrieveFeature #Get web scoped feature from site using feature id   

Enable Web Scope Feature:

The feature can be activated, using PnP PowerShell script. Enable-SPOFeature command is used to activate the features. Along with the command, the necessary parameter is the identity. The identity is the feature Id. The scope is an optional parameter.

The code snippet, given below, helps in activating the feature at the Web scope.
  1. function EnableFeature(){  
  2.     # Active Mobile View web feature  
  3.     Enable-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web  
  4.   
  5.     $feature = Get-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web
  6.     if($feature.DisplayName -ne $null){  
  7.         Write-Host $feature.DisplayName " feature is activated"  
  8.     }  
  9.     else{  
  10.         Write-Host "Feature is not activated"  
  11.     }  
  12. }  
  13. EnableFeature # Activates Mobile View web feature using feature id   

The image, given below, shows the operation.



The image, given below, shows the activated feature. The URL is required to access the Web scope feature.




Disable Web Scope Feature:

The feature can be deactivated, using PnP PowerShell script. Disable-SPOFeature command is used to deactivate the features. Along with the command, the necessary parameter is the identity. The identity is the feature Id. The scope is an optional parameter.

The code snippet, given below, helps in deactivating the feature at the Web scope.
  1. function DisableWebFeature(){  
  2.       
  3.     Disable-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web  
  4.       
  5.     $feature = Get-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web
  6.     if($feature.DisplayName -eq $null){  
  7.         Write-Host $feature.DisplayName " feature is deactivated"  
  8.     }  
  9.     else{  
  10.         Write-Host "Feature is still active"  
  11.     }  
  12. }  
  13. DisableWebFeature # Deactivates Mobile View web feature using feature id  
Note: The operations will be compatible for SharePoint 2013/2016 On Premise / SharePoint online sites.

Wednesday 29 June 2016

Create, Retrieve, Update Or Delete Sites On SharePoint Using PnP PowerShell

In this post, you will learn, how we can create, retrieve, update and delete SharePoint sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite:

You need to have PowerShell 3.0, available on Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or cmdlets from the official site.

The following operations will be compatible for SharePoint 2013 / 2016 on-premise or Office 365 versions.

Connect To Site:

Connect to the site, using the snippet, given below. The below PnP PowerShell code helps in getting the current context of the site, using the client side object model (CSOM).
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext   
Once connected, you can carry out any of the operations, mentioned below, based on the requirement.


Create Sub Site:

From the existing site collection, the sub sites can be created. The same can be accomplished, using PnP CSOM PowerShell.

New-SPOWeb command is used to create sub sites on SharePoint sites. The necessary parameters to create the site are Title, URL and template. Other parameters, that can be passed to create site are description, break inheritance etc.

There are two ways to create a site. When the command New-SPOWeb is entered on the PowerShell, you will be prompted for the parameters. The site will be created.

Other way of creating the site is to pass the parameters along with the command. The following command helps in creating a sub site.
  1. New-SPOWeb -Title "PnPSite1" -Description "PnPDescription1" -Url "PnPSite1" -Template "STS#0"    


Retrieve Site or Sub Site:

From the site collection, the sub site properties can be retrieved. The same can be accomplished, using PnP CSOM PowerShell.

Get-SPOWeb is used to retrieve the sub site properties. Along with the command, the sub site name or the URL is passed to retrieve the information. When the command is just passed without any parameters, the current site properties will be retrieved.

The basic properties which can be retrieved using the command are title, GUID, URL.

The following PowerShell code snippet helps to retrieve the site properties:
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  
  4. #Get Current Context Site (Root)  
  5. function RetrieveSite(){  
  6.     $web = Get-SPOWeb  
  7.     Write-Host "Title : " $web.Title  
  8.     Write-Host "Description : " $web.Description  
  9.     Write-Host "URL : " $web.Url  
  10. }  
  11. #Get Sub Site  
  12. function RetrieveSubSite(){  
  13.     $web = Get-SPOWeb "PnPSite1"  
  14.     Write-Host "Title : " $web.Title  
  15.     Write-Host "Description : " $web.Description  
  16.     Write-Host "URL : " $web.Url  
  17. }  
  18. RetrieveSite #Get Current Context Site (Root)  
  19. RetrieveSubSite #Get Sub Site   


Update Site:

From the site collection, the sub site properties can be updated. The same can be accomplished, using PnP CSOM PowerShell.

To change the site properties, retrieve the corresponding site, using title or URL. The parameters are not required, if you are trying to change the current context site (root) properties.
Update the site with the necessary parameters, using Set-SPOWeb.

The parameters that can be changed are title, site logo URL.

The following PowerShell snippet helps to update the site or sub site properties.
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  
  4. #Update Current Site  
  5. function UpdateSite(){  
  6.     $web = Get-SPOWeb  
  7.     Set-SPOWeb -Web $web -Title "Title Update"  
  8. }  
  9. #Update Sub Site  
  10. function UpdateSubSite(){  
  11.     $web = Get-SPOWeb "/PnPSite1"  
  12.     Set-SPOWeb -Web $web -Title "PnPSite1 Update"  
  13. }  
  14.  
  15. # Call above Functions  
  16. UpdateSite #Update Current Site  
  17. UpdateSubSite #Update Sub Site  

Delete Site:


From the site collection, the sub sites can be deleted. The same can be accomplished, using PnP CSOM PowerShell.

The parameter that needs to passed is an identity. By using Remove-SPOWeb command, the site or sub site can be deleted.

The following PowerShell snippet helps to delete the site from the site collection.
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  
  4. # Delete site  
  5. function DeleteSubSite(){  
  6.     Remove-SPOWeb -Identity "PnPSite1" -Force      
  7. }   

Tuesday 28 June 2016

Create, Retrieve, Update Or Delete Sub Sites On SharePoint Using JavaScript Object Model

In this post, you will learn how to retrieve, create, update or delete the sites under a site collection, using JavaScript Object Model (JSOM) on SharePoint 2013 / SharePoint Online sites like O365. The sites are considered as the sub sites within the site collection.

Before executing any operation, load the required scripts like SP.js, SP.RunTime.js. If the scripts are not initialized, use the snippet, given below:
  1. var scriptbaseURL = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";    
  2. $.getScript(scriptbaseURL + "SP.Runtime.js",    
  3.     function ()  
  4.    {    
  5.         $.getScript(scriptbaseURL + "SP.js", RequiredFunctions);    
  6.     }    
  7. );   


Create Sub Site:

The operation will create a sub site within the site collection. 
  • Access the context of the site and the Web object, using root site URL or the current context.
  • Create WebCreationInformation Object. Set the necessary parameters like title, description, URL, Web template, site permission and the language.
  • Add it to the existing Web collections and update the current site. Execute the request.
The code snippet, given below, helps in creating a site.
  1. var rootSiteUrl = "https://abc.sharepoint.com";  
  2. var ctx = new SP.ClientContext(rootSiteUrl);  
  3. var web = ctx.get_web();  
  4. var webInfo = new SP.WebCreationInformation();  
  5. webInfo.set_title('subsite');  
  6. webInfo.set_description('subsite description');  
  7. webInfo.set_url('subsite');  
  8. webInfo.set_webTemplate('STS#0');  
  9. webInfo.set_useSamePermissionsAsParentSite(true);  
  10. webInfo.set_language(1033);  
  11.   
  12. web.get_webs().add(webInfo);  
  13. web.update();  
  14. ctx.executeQueryAsync(function(){  
  15.     console.log("Sub Site Created");  
  16. },  
  17. function(sender, args){  
  18.     console.log("Failed to create sub site : " + args.get_message());  
  19. }  
  20. );   


Retrieve Sub Site:

The site properties can be retrieved, using JSOM. 
  • Access the context and the Web object, using sub site URL.
  • The basic properties can be retrieved from the above Web object.
  • Other properties and information can be retrieved by accessing the necessary properties and methods. For example, just loading the Web object will not be able to retrieve more information on the features. To get the active features on the site, we need to access the feature on the select and load the object to retrieve the features information. Similarly, other collection values needs to be retrieved by explicitly accessing the methods. 
The code snippet, given below, helps to retrieve the site properties.
  1. var subSiteUrl = "https://abc.sharepoint.com/subsite";  
  2. var ctx = new SP.ClientContext(subSiteUrl);  
  3. var web = ctx.get_web();  
  4. var features = web.get_features();  
  5. // Similarly other properties and information can be retrieved from the site  
  6. ctx.load(web);  
  7. ctx.load(features);  
  8. ctx.executeQueryAsync(function(){  
  9.     console.log("Title : " + web.get_title());  
  10.     console.log("Description : " + web.get_description());  
  11.     console.log("Totally " + features.get_count() + " Features active on the site")  
  12.     // Similarly other feature properties can be retrieved.  
  13. },  
  14. function(sender, args){  
  15.     console.log("Failed to retrieve sub site info : " + args.get_message());  
  16. }  
  17. );   


Update Sub Site:

The update operation will be very similar to create an operation. 
  • Access the context of the site and the Web object, using sub site URL.
  • Update the necessary properties, using the set methods.
  • Update and execute the request. 
The code snippet, given below, helps in updating the site properties. Here, title and the description of the sub site are updated.
  1. var subSiteUrl = "https://abc.sharepoint.com/subsite";  
  2. var ctx = new SP.ClientContext(subSiteUrl);  
  3. var web = ctx.get_web();      
  4. web.set_title('new subsite');   
  5. web.set_description('subsite description updated');  
  6. web.update();  
  7. ctx.executeQueryAsync(function(){  
  8.     console.log("Sub site properties updated");  
  9.     console.log("Title : " + web.get_title());  
  10.     console.log("Description : " + web.get_description());  
  11.  },  
  12.  function(sender, args){  
  13.     console.log("Failed to create sub site : " + args.get_message());  
  14.  }  
  15. );   


Delete Sub Site:

The delete operation will be very similar to create or update operations. Here, no input data is required. 
  • Access the context of the site and the Web object, using sub site URL.
  • Using the Web object, delete the site with delete object method.
  • Execute the request, using the client context. 
The code snippet, given below, helps to delete the site.
  1. var subSiteUrl = "https://abc.sharepoint.com/subsite";  
  2. var ctx = new SP.ClientContext(subSiteUrl);  
  3. var web = ctx.get_web();      
  4. web.deleteObject();  
  5. ctx.executeQueryAsync(function(){  
  6.     console.log("Sub site deleted");          
  7. },  
  8. function(sender, args){  
  9.     console.log("Failed to create sub site : " + args.get_message());  
  10. }  
  11. );  

Note: In these cases, you can check the result values in a Browser debugger console. You can change the logic to display the results on the pages. Place these scripts on the page, using the Web parts (Content Editor / Script Editor / any custom Web part) at the site collection level.