Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 1

Today I am going to start a new series of automating some of the SharePoint online (O365) tasks using the PowerShell and Client Side Object Model (CSOM). Anyone who have worked on SharePoin Online knows that there are not as many PowerShell cmdlets as On-Prem. So if you used to automate tasks using PowerShell in On-Prem scenario, you will soon find that SharePoint Online does not have that flexibility. The option is to extend this using PowerShell and CSOM.
I am not going to be going in too much detail on this as Chris O’Brien have posted a really good blog post on this. You should read that article first before proceeding.
Basically what I am going to post is code on different SharePoint Online tasks. In next few parts what I will be covering is how you can create Content Types, Site Columns, Add Site Columns to Content Type and then Add Content Type to a List.
In this blog post I will show you, how you can access a web and then a list using PowerShell and CSOM. Once  you get the list how you will change Allow Management of Content Type option to True.
So lets start.
First step is to Authenticate against your O365 SharePoint site. If you have read the blog above by Chris, it has the code for the same. I would say create a PS1 file using that code and then use that code in your any other code. So for the sack of this blog post I am assuming that you copied the code from above blog post and called that PowerShell AuthenticateO365.ps1.
So next step is to Get List and then enable Allow Management of Content Type option. Here is the code for that.
# ……START Authenticate………………..
    .\1_Authenticate.ps1
    #……..END Authenticate…………………
    #……..START – Update List……………………
    $web = $ctx.Web 
    $list = $web.Lists.GetByTitle(“SPSBoston”)
    $ctx.Load($web) 
    $ctx.Load($list)
    $ctx.ExecuteQuery()
    Write-Host “Current list title is ‘$($list.Title)'” -ForegroundColor Yellow
    Write-Host “”
    $list.ContentTypesEnabled = $true
    $list.Update()
    $ctx.Load($list)
    $ctx.ExecuteQuery()
    Write-Host “List Content Type Enabled: ” $list.ContentTypesEnabled -ForegroundColor Yellow
    #……..END – Update List……………​

Updating List Template after Saving List as Template

Many time you will run into a situation where you will save a list and/or library in SharePoint as a template. After saving list/library as template you may want to change some of the settings e.g. You saved document library as template which had Major Versioning enabled. Now you want to turn minor versioning enabled as well. 
One way to handle this is to make change in the document library and save it again as a template again. 
I am going to show you how you can actually modify the existing library template and re-upload it again. Following are the steps.
Note: In this example I have saved document library as template and I am going to change Versioning setting on library level.
1) Download the Document Library Template from the list template gallery and save it to local drive e.g. C:\Temp
2) All list templates are CAB files so you can simply rename list template (STP) file to CAB file.
3) Now that you have CAB file extract the CAB file that will give you manifest.xml file.
4) Open manifest.xml file in Visual Studio for editing. For enabling major and minor version in document library template make sure you include VersioningEnabled and EnableMinorVersions properties and set them to TRUE. Here is the example:
<List Name=”{CB2342B1-0650-4059-857E-F8916A954C70}” Title=”Documents” Description=”This system library was created by the Publishing feature to store documents that are used on pages in this site.” Direction=”0″ BaseType=”1″ FeatureId=”{00BFEA71-E717-4E80-AA17-D0C71B360101}” ServerTemplate=”101″ Url=”Documents” VersioningEnabled=”TRUE” EnableMinorVersions=”TRUE” MajorVersionLimit=”0″ MajorWithMinorVersionsLimit=”0″ 
5) Once you make the change save the file. Now you are going to reverse engineer to create new CAB file from scratch. We will use MakeCAB.exe for this.
6) First open a NotePad and paste the following code in it.
OPTION EXPLICIT ; 
.Set CompressionType=MSZIP ;
.Set Cabinet=on
; Destination Folder
.Set DiskDirectory1=c:\Temp
; File name for the new cab file. This will be your List Template name.
.Set CabinetNameTemplate=SharedDocuments.cab  
; Files to be added are listed below
c:\Temp\manifest.xml
7) Save file as docTemplate.ddf and then run the following command to create CAB file.
makecab /f c:\Temp\docTemplate.ddf
8) This should give you  your CAB file. Next step is to simply rename the CAB file to STP and upload it back to list template gallery.

 

REST API How To: Get specific user permission for specific item in Sharepoint 2013

I have been spending some time with REST APIs when working with SharePoint 2013. Today I am going to share with you some RESP API code samples related to getting SharePoint Site/List/List Item permissions using the REST API.

To get permission for specific site for particular user you can use

http://intranet.contoso.com/_api/web/getusereffectivepermissions(@user)?@user=%27i%3A0%23%2Ew%7Ccontoso%5Cbrianc%27

Here the username is in this format i:0#.w|contoso\brianc for Windows Claims. This will change for SAML Claim

To get specific list permission for particular users you can use

http://intranet.contoso.com/_api/web/lists(guid’F27BDE7D-340F-43F4-B606-D24E02D0D01C’)/getusereffectivepermissions(@user)?@user=’i%3A0%23%2Ew%7Ccontoso%5Cbrianc’​


​And finally to get permission for a list item for particular user you can use

http://intranet.contoso.com/_api/web/lists(guid’F27BDE7D-340F-43F4-B606-D24E02D0D01C’)/getitembyid(16)/getusereffectivepermissions(@user)?@user=’i%3A0%23%2Ew%7Ccontoso%5Cbrianc’​

​Over next few months I will be sharing few more blog posts on REST API HOW TO Series.

Cheers.

SharePoint Saturday Boston – 2014

​Last Saturday, on April 12th, I presented at the SharePoint Saturday Boston 2014. This was my first time speaking at SharePoint Saturday Boston.

The event was at Microsoft Boston office and I must say it was a great venue.

I presented a session in the Office 365 track on how to automate Office 365 using PowerShell and Client Side Object Model (CSOM). The event was very well organized and I met many old friends and made some new.
I would like to thank everyone who attended my session. Please find my presentation slides below.
Kudos to all the organisors, sponsors and volunteers for successful event!!!

PowerShell How to: Get Audience membership for a User in a Web

If you want to get Audience membership for a particular user then you can use GetUserAudienceIDs method. In this method you can pass the UserName of the user.

I have wrote a sample PowerShell script to achieve this.

$site = GetSPSite “http://SharePoint-2013”

$web = $site.Openweb()

$context = [Microsoft.Office.Server.ServerContext]::GetContext($site)

$audMgr= newobject Microsoft.Office.Server.Audience.AudienceManager($context)

[System.Collections.ArrayList]$audIDNames = audMgr.GetUserAudienceIDs(“Contoso\\Garthf”, $true, $web);

$audNameID = newobject Microsoft.Office.Server.Audience.AudienceNameID()

for ($i=0;$i lt $audIDNames.Count; $i++)

{ WriteHost $audIDNames[$i].AudienceName }

SharePoint and Permissions for Style Resource Readers Group

​This one is interesting as someone asked me this question as to what permission does the Style Resource Readers group need access to?

Here is what you need to know about style resource readers group.

When you create a new site collection SharePoint assigns the required permission to Style Resource Readers group. By default it will read Limited Access. You do not need to modify anything with Style Resource Reader group unless you are planning on adding users to that group. Personally I will advise against it.

Limited Access in SharePoint means that there are permission assigned to a user or group on one or more site artifacts i.e. list, folder, item but no permission is assigned to access site.

So Style Resource Readers group out of the box has the Read permission to master page gallery. If you read the description it reads.

“Members of this group are given read permission to the master page gallery and the Restricted read permission to the Style Library. By default, all authenticated users are a member of this group.”

So don’t worry about this group and leave it as is given SharePoint already assigned required permission at the master page gallery and style library level for this group.