Wednesday, March 25, 2015

Create Site Columns in SharePoint Online (Office 365) using CSOM

cls

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

# Create a context and return the context object.
function getContext()
{
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
    $context.Credentials = $credentials

    return $context;
}

# Check if you are able to connect to SharePoint online.
function validateSPOnline()
{
    try
    {
        
        $ctx = getContext;
        $web = $ctx.Web;
        $ctx.Load($web);
        $ctx.ExecuteQuery();

         return $true;
     }
     catch
     {
         Write-Host "Unable to connect with SharePoint online site. Error description: " $_.Exception.Message -ForegroundColor Yellow;
         break;
     }
}

# Create Site Columns by reading a xml file: sitecolumns.xml
function createAllSiteColumns()
{
    $ctx = getContext;
    $web = $ctx.Web;

    # Read Fields from Xml Template
    $siteColumnsXml = [xml](get-content $file_SiteColumns)

    Write-Host "Creating site column(s)..." -f Cyan
 foreach ($node in $siteColumnsXml.Template.Field) 
    {
        $fieldName = $node| Select DisplayName
        $web.Fields.AddFieldAsXml($node.OuterXml, $false, [Microsoft.SharePoint.Client.AddFieldOptions]::DefaultValue);
        $ctx.ExecuteQuery();
    }

    $ctx.Dispose();
    $ctx = $null;
}

function main()
{
    if (validateSPOnline)
    {
        createAllSiteColumns;
    }
}

#SharePoint Online Settings
#$siteUrl = Read-Host 'Please enter the SharePoint online site url'
#$username = Read-Host 'Please enter the username'
#$password = Read-Host "Enter password" -AsSecureString
$siteUrl = “https://spapps.sharepoint.com”
$username = "muhammadsalman@SPApps.onmicrosoft.com"
[string]$pwd = “Passwort123456“
$password = $pwd | ConvertTo-SecureString -AsPlainText -Force

# File Paths
$file_SiteColumns = "C:\Users\Projects\SPApp-Zeiterfassung\List Creation\SiteColumns.xml"

main


Site Columns XML file


Friday, March 6, 2015

Hide Fields (columns) from SharePoint List Forms based on Permission (Groups) using JSOM

Context: Customer wants to hide some fields on the new (NewForm.aspx), edit (EditForm.aspx) and display (DispForm.aspx) forms of the SharePoint list from the members based on their permissions.

Solution: Since the customer is using SharePoint online site, I wrote a small JSOM script to fulfill their requirements. I placed the name of the site groups along with the fields in a hash table (array). In this way, I knew which fields I need to hide from the members of group. Furthermore, if a user belongs to two groups then permissions from the group with higher permissions should apply to the user. So, i placed the groups in a separate array which means that one can find group with hishest permssion at index 0.
The following lines of code that I wrote and then later added a Script Editor Webpart and placed the code in it;



Note: The above code will work on DispForm.aspx. But, if you want to make it work for edit or new forms then you have to find the html controls on the forms. For instance; for edit form, you have to make changes in the hideHtmlElements function as shown below and rest of the code will work for edit form;
function hideHtmlElements(elementsArray)
{
     $(elementsArray).each(function(index, value){
         if(value != "")
         {
       var elementFound = $("nobr:contains(" + value + ")");
       var parentTr = elementFound.closest("tr");
       parentTr.hide();
         }
     });  
}