Wednesday 17 August 2016

Office 365 - Manipulate Items Programmatically (using Powershell)

A - OBJECTIVE: 

Developers and administers are expected to manipulate objects at SharePoint Online (aka Office 365), such as:
  • compare files
  • manage permissions
  • etc
This post is to elaborate the steps to do administrative work programmatically by Powershell.

B - PROBLEMS :

Manual work at Office365 is not a good idea. Using Powershell, you can improve your productivity (speed) to deliver the work faster.

You can run the script from your own PC (e.g. Windows 7) with the following prerequisites:

  1. Download & Install "Powershell 3.0": https://www.microsoft.com/en-us/download/details.aspx?id=34595
     
  2. Download & Install “SharePoint Online Management Shell”
     
  3. Download & Install “SharePoint Server 2013 Client Components SDK” https://www.microsoft.com/en-gb/download/details.aspx?id=35585
  4. Download & Install "SharePoint Online Client Components SDK" at https://www.microsoft.com/en-us/download/details.aspx?id=42038 

    C - SOLUTION:

    The required API reference is Microsoft.SharePoint.Client (Microsoft details) which is integrated into the SharePoint Online Management Shell. This will allow you to manipulate objects at SharePoint Online sites.

    D - SOURCE CODE:

    An example to list items (level 1) of a document library at SharePoint Online site:

    Step 1: provide the information of your sites & the credential 



    Step 2: setup the connection to SharePoint Online



    Step 3: access to the specific library in a web



    Step 4: design a CAML query to extract relevant items




    Step 5: load the items from the current context and display them





    Powershell Script:


    #variables that needs to be set before starting the script
    $siteURL = "https://tenant.sharepoint.com/sites/git"
    $adminUrl = "https://tenant-ADMIN.sharepoint.com"
    $userName = "admin@tenant.onmicrosoft.com"
    
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
     
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)  
    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
     
    $web = $context.Web
    $list = $web.Lists.GetByTitle("4. IT Capabilities and Knowledge Base")
    
    # CAML QUERY REFERENCE: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.getitems.aspx
    $query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $query.ViewXML = ""
    
    # LIST ITEMS
    [Microsoft.SharePoint.Client.ListItemCollection]$items = $list.GetItems($query)
    $context.Load($items)
    try{
     $context.ExecuteQuery()
    }
    catch{
     write-host "info: $($_.Exception.Message)" -foregroundcolor red
    }  
    
    foreach ($item in $items) { Write-Host $item.Id " - " $item["Title"] }
    
    


    Note: 

    (1) all required DLLs are loaded at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI

    (2) you can use SharePoint Online Management Shell to connect to your on-premises SharePoint farm as well 

    $siteUrl = "http://yourOnPremSP/site"
    $loginname = "spadmin@yourdomain.com"
    $pwd = Read-Host -AsSecureString
    
    # initialize the environment
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    
    # IMPORTANT NOTE: NetworkCredential Class must be used
    $ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd)
    
    $web = $ctx.Web 
    $ctx.Load($web) 
    $ctx.ExecuteQuery() 
    
    Write-Host " Current web title is '$($web.Title)', $($web.Url)"
    
    

     

    No comments:

    Post a Comment