Site Provisioning using PnP

Recently I got one requirement from one of my client that they want to re-use the artefacts of one site across multiple site instances. There were some other options but they have their own limitations. For ex: “Save Site as Template” option is not visible now in site settings in case of publishing sites.

So for this requirement we thought of going with “Site Provisioning” using PnP Provisioning engine.

You will find lot many examples on web which uses PnP core powershell extension but very few examples with C#.

In this post I first got the site artefacts using “GetProvisioningTemplate()” method then I used “ApplyProvisioningTemplate()” method to provision site. This code will work both for Team Site and Publishing site. I haven’t tested this with other site templates but it should work.

We need to add a NuGet package for PnP Library. So we can add “SharePointPnPCoreOnline” in our solution.

pnpcoreonline

By adding this NuGet, required methods for getting/applying provisioning template will be available.

I have used below method to create the template.

While executing above code I found out that page layouts and master pages were not downloaded but were present in the xml generated. So while applying template it was giving error. So to download the missing files I used the property “PersistPublishingFiles” of “ProvisioningTemplateCreationInformation” class.

I have used below method to Apply Template on new site.

I have saved the xml file generated at below location “C:\temp\pnpprovisioningdemo”, so I am providing this path while applying template. You can change this path as per your xml file location.

Hope this helps you in Site Provisioning. Happy Coding !!

References:

PnP-Guidance Provisioning

Pnp-Provisioning Sample

Advertisements

REST URL’s For SharePoint

This post is a collection of REST endpoints which are very helpful in performing various operations in SharePoint. Sometimes while working on projects we come across various scenarios where we have to use REST but we don’t know if such thing is possible or not using REST or whether any endpoint is available in REST for this.

I will try to cover some scenarios where i used REST and they are pretty useful and easy to use. I will keep on updating this post as and when possible.

If you know some endpoints which are really very useful and are not mentioned in this post, you can suggest them in comments. I will try to incorporate it in my post.

Endpoints that are helpful in read operation map to HTTP GET. Update operation map to HTTP POST, for Update and Insert operations map to HTTP PUT and to Delete SharePoint object it maps to HTTP DELETE.

List Operations:

  • Url to get all items:

/_api/Web/Lists/GetByTitle(‘List Title’)/Items

  • Select query on List to get Selected Items:

/_api/Web/Lists/GetByTitle(”List Title)/Items?$select=column1,column2

  • To get user details like ‘FirstName’, ‘LastName’, ‘EMail’, etc for a Person/Group field of SharePoint List we need to make use of ‘expand’ in REST API. In REST API we get id instead of actual value because person/group field is a lookup field internally.

/_api/Web/Lists/GetByTitle(‘List Title’)/Items?$select=column1,column2,column3/EMail&$expand=column3/Id

listitemexpand

  • Get Attachment from List Item

_api/Web/Lists/GetByTitle(‘UserData’)/items?$select=AttachmentFiles&$expand=AttachmentFiles

listitemattachment

 Working With Libraries

  •  You can retrieve the root folder of your Document Library using the following endpoint:          

/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)

  • To retrieve all files in a folder:

/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)/Files

  • To retrieve a specific file:

   /_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)/Files(‘Test.docx’)

documentlibfilejpg

Get User Properties with REST api

  • Get properties of current user

_api/SP.UserProfiles.PeopleManager/GetMyProperties

  • Get specific properties for current user

_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email,DisplayName,PictureUrl,AccountName

SP.SOD.executeFunc vs SP.SOD.executeOrDelayUntilScriptLoaded

Recently i was working on a POC and i was in need of loading “SP.UserProfiles.js” for my code. So i used below code to load “SP.UserProfiles.js” and to my surprise it didn’t worked.

Since above code seems to be working perfectly fine, i was not able to figure out why this is not working and after little googling i found out the issue.

SP.SOD.executeFunc supports on demand scripts while SP.SOD.executeOrDelayUntilScriptLoaded does not support. So above code will not work if “SP.UserProfiles.js” is not loaded.

If we are using SP.SOD.executeFunc then there is not need to reference JavaScript file using SharePoint:ScriptLink in master page.

Now see the below lines of code which worked fine for me.

Happy coding.!!

Making REST calls using C#

Here is how to make REST calls using C# code. It’s easier to make REST call using JavaScript but it is little bit tricky to make REST calls using C#.

I will show simple example to update list item using REST in a console application.

While updating or Creating list items we need Form Digest. So in below code, first of all we will have to get Form Digest using method GetFormDigest().

This is complete program.cs. This is tried and tested code and it works well.

Let me know your thoughts on comments below.

Enjoy reading.