Sunday, 14 February 2016

Office 365 / SharePoint Online : Creating the Site Columns / Fields using CSOM in PowerShell

Hi All,

Recently we got a chance to create the site columns for our one of the SharePoint online site collection. Rather than creating them manually, we have used the approach of CSOM in PowerShell scripts and reading all the fields/site columns from the .CSV file.
 
So in this article I’ll explain the steps how to create the Site Columns / Fields, reading from the .CSV file using CSOM in PowerShell.
 
Step 1 : Preparing the .CSV file having all the site columns and values for their respective properties which we want to set.

For example below .CSV file which we are using in this script
Fig 1 : Sample fields CSV file and fields properties
In above .CSV file, column headings are basically the properties of the site columns. We can add any property as column which we want to set. Each row represent the site column and their properties values.
 
Step 2 : Set the required variables like SiteCollection URL where we need to create the site columns, UserName and Password of your site to connect as
 
#Site collection URL where we need to create the site columns
$siteurl = ""  # Your site collection URL
 
#User name and Passwords
$userName =""  # Your user name
$password =""  # Password
 
Step 3 : Get the Microsoft.SharePoint.Client.ClientContext instance and set the credentials as
 
#client context object and setting the credentials 
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl
 
 # convert password into secured string
$securedpw = ConvertTo-SecureString $password -AsPlainText -Force

$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpw)
 
Step 4: Get / Load the required web object, we will require it to get the fields collection as

#get the respective web
$site = $clientContext.Site
$web = $site.RootWeb

#Get all fields collection
$fields = $web.Fields

$clientContext.Load($web)
$clientContext.Load($fields)

$clientContext.ExecuteQuery()

Step 5 : Import the site columns .CSV file

# Import the site columns CSV file
$fieldslist = Import-Csv -path 'C:\CreateSiteColumns.csv'  #give here your .csv file path

Step 6 : Loop through above fields list and create the site column

#Create the respective fields
ForEach($field in $fieldslist) {

# Prepare the fieldxml for Site Column - Please be careful with single and double quotes, if those got disturbed then it take some time to fix :)

$fieldXML = "<Field Name='" + $field.Name +"' Type='" + $field.Type +"' Description='"+$field.Description+"' DisplayName='"+$field.DisplayName+"' StaticName='"+$field.StaticName+"' Group='"+$field.Group+"' Hidden='"+$field.Hidden+"' Required='"+$row.Required+"' Sealed='"+$field.Sealed+"' ShowInDisplayForm='"+$field.ShowInDisplayForm+"' ShowInEditForm='"+$field.ShowInEditForm +"' ShowInNewForm='"+$field.ShowInNewForm+"'></Field>"

# Create Site Column from fieldXML
$fields.AddFieldAsXml($fieldXML,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
 
$clientContext.Load($fields)
    
 
} #foreach
 
$clientContext.ExecuteQuery()
 
Thanks!

Enjoy Reading :)

Feel free to comment / feedback / suggestions / thoughts if any or if you have any query :) 

Thursday, 11 February 2016

SharePoint Online / Office 365 - SP.JS file is not getting not loaded for read only users (users having “read” / “View” permission)

Hi All,

In this article I’ll explain the issue which we faced for users who have read /view rights on the site for SP.JS file and workaround we did for it.
 
Background : We have document library and two level folders so hierarchy is like
 
                         Document Library => Folder => Sub Folder1, Sub Folder 2, Sub Folder 3
 
Our requirement is on one publishing page we need to fetch all sub folders and want to show them with links. So that user will click on respective sub folder link and directly go to the respective sub folder either to view the documents or to upload based on the permissions.
 
So we have written JavaScript file, used JSOM (Java Script Object Model), we queried to the folder directly and listed the sub folders. Added Content Editor web part on the page and refer the our JS file. All is fine and working.
 
Problems occur when user having only “read” / “view” permission visited the page he/she cannot see the result. But Site Collection Administrators or user having Full Control / Contributes rights can see the result.
 
Problem / Issue :
Since it is JS file, we started debugging using Developer Tool bar in IE and since we have code like as follow
$(document).ready(function () {
          ExecuteOrDelayUntilScriptLoaded(loadSubFolders,"sp.js");
                });
 
our function “loadSubFolders” never get called since “sp.js” file is not loaded and if we directly call function like
 
               $(document).ready(function () {
                        loadSubFolders();
                });
 
then while getting object of ClientContext(as shown in following line) in function we got the exception.
 
                      var context = new SP.ClientContext.get_current();
 
So verified all the .JS files loaded and “SP.JS” and “SP.Runtime.JS” files were not there as shown in Fig 1. “sp.core.js”, “sp.init.js” files are loaded but not “sp.js” and “sp.runtime.js” loaded.
 
Figure 1 : Only these .js files are loaded
We went to our master page, verified there we have explicitly loaded the “sp.js” file and “sp.runtime.js” files as
 
Figure 2 : sp.js and sp.runtime.js referenced in master page
We were wondering why these two files are not getting loaded.
 
Solution / Work Around : After googling a lot we found that “sp.js” file is loaded only for specific permissions and it doesn't get loaded for the users who have View / Read permissions (in some cases). Sometime it works only for Edit / Approve permissions and suggestion like use of explicitly calling “sp.js” using SP.SOD.executeFunc .
 
So we tried this in our js file but then it started throwing some other exceptions but not working. Our other JSOM web parts also failed.
 
Finally we tried one workaround, we have added one Script Editor web part, and added following snippet into it, published the page and it worked like charm :)
<script type="text/javascript">
   SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
   //Empty function just to ensure "sp.js" file loaded
  function sharePointReady(){
}
        </script>

Thanks!
Enjoy Reading :)
As usual any comment / suggestion / feedback / questions always welcome :)