Showing posts with label CSOM. Show all posts
Showing posts with label CSOM. Show all posts

Sunday 20 January 2019

CSOM: Working with Office365 Project Online Tasks - Part II

This article explains steps for adding, updating or deleting a task to/from Office 365 Project Online plan schedules.

This article series focuses on working with  office 365 project online plan schedule tasks using Client Side Object model approach. In the previous post, we have seen multiple ways of retrieving tasks from the project plans.



Adding a Task: 


A task can be added to the Office 365 project plan schedule by the following steps.
  • Required project needs to be checked-out before any operation. 
  • Task collection is retrieved from the draft project. 
  • Task creation object is created with necessary OOB field values, and will be added to the task collection. 
  • Then the draft project will be updated and published. 

Friday 11 January 2019

CSOM: Working with Office365 Project Online Tasks - Part I

This article series focuses on working with office 365 project online plan schedule tasks using Client Side Object model approach.

Previously we have seen how the same operations works using JSOM approach. Both approaches are identical.


Creating Console App and Importing Package: 


SharePoint and Project Server online libraries are required to work with the project server data using CSOM model. The libraries can be downloaded from Nuget packages. The required libraries are packaged under one single package. i.e., Microsoft.SharePointOnline.CSOM

Create a visual studio console application. From Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution. From the browse tab, Find for “Microsoft.SharePointOnline.CSOM”. Select and install the package for the current solution.

Saturday 18 June 2016

Add Custom Tabs and Groups to the Ribbon on SharePoint using CSOM PowerShell


In this post, you will learn how to add custom ribbon tabs, ribbon groups and ribbon items (user actions) for for list using CSOM Powershell on SharePoint 2013 / SharePoint 2016 / SharePoint online sites.


Steps Involved:

The following prerequisites are required to be executed, before going for any operations, using CSOM PowerShell on SharePoint sites.
  1. Add the references, using the Add-Type command with the necessary reference paths. The necessary references are Client.dll, Client.Runtime.dll and publishing.dll.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    3. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"  
  2. Initialize the client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. If you are trying to access SharePoint Online site, then you need to setup the site credentials with the credentials parameter and load it to the client context. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Not required for on premise site - End  
  4. If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.
    1. #Credentials for on premise site - Start  
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    3. $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)  
    4. $ctx.Credentials = $creds  
    5. #Credentials for on premise site - End  
We will see, how we can add the custom ribbon tabs and other components to the SharePoint list ribbon.
  • Get the existing user action collection. Access the Web and get the existing user actions, using UserCustomActions property. Load and execute the property.
  • Add new custom action to the user actions collection. The necessary parameters for new action are registration ID, title, registration type and location. Here we need to set the ribbon tab values.
    • Registration ID - Corresponding List template ID to add the user action. For example, here 100 denotes the custom list templates.
    • Registration Type - Denotes association type.
    • Location - Custom action location.
  • Build the XML for the new extension. The following are the steps considered. 
    • New custom tab has been added to the ribbon. 
    • Then new group has been associated with the tab created. 
    • Then custom control (button) is added to the group created.
  • Update and execute to see the changes.
The XML extension contains the following elements.
  • New definition for tab with specified ribbon location. The tab will contain scaling and groups components.
    • The scaling will denote the behaviour of tab (sizing). Size is associated to the layout title under the group template.
    • The groups can have one or more group elements. Each group will have the properties like title, description, template (associated to group template) and id. And group also contains controls to be associated. The controls can button, label, etc. 
      • For each control, set the necessary parameters. The command parameter is associated to UIHandler. The TemplateAlias should match alias name defined for the control under group template.
  • Another definition for group template needs to be created. Here, the new template details for the group, layout mapping and display mode (size) for controls are defined.
  • The UIHandler section contains necessary javascript function for the controls defined. 
The following piece of code shows the operation with necessary XML.
  1. $web = $ctx.Site.RootWeb  
  2. $userActions = $web.UserCustomActions  
  3. $ctx.Load($userActions)   
  4. $ctx.ExecuteQuery()   
  5.   
  6. $newRibbonItem = $userActions.Add()  
  7. $newRibbonItem.RegistrationId = "100"  
  8. $newRibbonItem.Title = "Custom Ribbon Tab"  
  9. $newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::List  
  10. #$newRibbonItem.Id = "Ribbon.CustomTab"  
  11. $newRibbonItem.Location = "CommandUI.Ribbon"  
  12.   
  13. $ribbonUI = '<CommandUIExtension>  
  14.                     <CommandUIDefinitions>  
  15.                         <CommandUIDefinition Location="Ribbon.Tabs._children">  
  16.                             <Tab Id="Ribbon.CustomTab" Title="Custom Tab" Description="Custom Tab with groups and user action" Sequence="100">  
  17.                                 <Scaling Id="Ribbon.CustomTab.Scaling">  
  18.                                     <MaxSize Id="Ribbon.CustomTab.CustomGroup1.MaxSize"  
  19.                                         GroupId="Ribbon.CustomTab.CustomGroup1"  
  20.                                         Size="LargeLayout" />  
  21.                                 </Scaling>  
  22.                                 <Groups Id="Ribbon.CustomTab.CustomGroups">  
  23.                                     <Group Id="Ribbon.CustomTab.CustomGroup1"  
  24.                                         Description="Collection of user actions"  
  25.                                         Title="CustomGroup1"  
  26.                                         Sequence="20"  
  27.                                         Template="Ribbon.Templates.LargeLayout">  
  28.                                         <Controls Id="Ribbon.CustomTab.CustomGroup1.Controls">  
  29.                                             <Button Id="Ribbon.CustomTab.CustomGroup1.ShowAlert" Alt="Show Alert" Sequence="101"  
  30.                                                  Command="ShowRibbonAlert"  
  31.                                                  LabelText="Custom Alert"  
  32.                                                  Image32by32="_layouts/15/images/alertme.png"  
  33.                                                  Image16by16="_layouts/15/images/alertme.png"  
  34.                                                  TemplateAlias="ButtonTemplate" />  
  35.                                         </Controls>  
  36.                                     </Group>  
  37.                                 </Groups>  
  38.                             </Tab>  
  39.   
  40.                        </CommandUIDefinition>  
  41.                         <CommandUIDefinition Location="Ribbon.Templates._children">   
  42.                             <GroupTemplate Id="Ribbon.Templates.LargeLayout">   
  43.                                 <Layout Title="LargeLayout" LayoutTitle="LargeLayout">   
  44.                                     <Section Alignment="Top" Type="OneRow">   
  45.                                         <Row>   
  46.                                             <ControlRef DisplayMode="Large" TemplateAlias="ButtonTemplate" />   
  47.                                         </Row>   
  48.                                     </Section>   
  49.                                 </Layout>   
  50.                             </GroupTemplate>   
  51.                         </CommandUIDefinition>  
  52.                     </CommandUIDefinitions>  
  53.                     <CommandUIHandlers>  
  54.                         <CommandUIHandler Command="ShowRibbonAlert"  
  55.                              CommandAction="javascript:alert(''hi'');"/>  
  56.                     </CommandUIHandlers>  
  57.                 </CommandUIExtension >'  
  58.   
  59. $newRibbonItem.CommandUIExtension = $ribbonUI  
  60. $newRibbonItem.Update()  
  61. $ctx.Load($newRibbonItem)  
  62. $ctx.ExecuteQuery()  

The following image shows the tab along with group and control.



Add Custom Ribbon Items To List Forms On SharePoint Using CSOM PowerShell


In this post, you will learn- how to add custom ribbon items (user actions) for various list forms, using CSOM Powershell on SharePoint 2013 / SharePoint 2016 / SharePoint online sites. The list forms considered are new form, display form, and edit form.


Steps Involved:

The following prerequisites are required to be executed, before going for any operations, using CSOM PowerShell on SharePoint ites.
  1. Add the references, using the Add-Type command with the necessary reference paths. The necessary references are Client.dll, Client.Runtime.dll and publishing.dll.

    Add-Type -Path 

    "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll

    Add-Type -Path

    "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll

    Add-Type -Path 

    "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll
  2. Initialize the client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. If you are trying to access SharePoint Online site, then you need to setup the site credentials with the credentials parameter and load it to the client context. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Not required for on premise site - End  
  4. If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.
    1. #Credentials for on premise site - Start  
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    3. $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)  
    4. $ctx.Credentials = $creds  
    5. #Credentials for on premise site - End  
We will see how we can add the custom ribbon items (actions) to the SharePoint list forms. The following are the steps, common to any list form. The code snippets, for adding ribbon items to various forms are shown in the sections, below:
  • Get the existing user action collection. Access the Web and get the existing user actions, using UserCustomActions property. Load and execute the property.
  • Add new custom action to the user actions collection. The necessary parameters for new action are registration ID, title, registration type and location.
    • Registration ID - Corresponding List template ID to add the user action. For example, here 100 denotes the custom list templates.
    • Registration Type - Denotes association type.
    • Location - Custom action location.
  • Build the XML for the new definition with corresponding handler. In this case, I have considered button as a custom action and associated alert box to the action. You can build your own logic on the command action parameter. Location in the XML denotes the place, where item will be placed in the ribbon. 
  • Update and execute to see the changes.
The following steps shows us the code and the parameter values to the respective forms.


Adding actions to Display Form:
  • The ribbon custom action location should be CommandUI.Ribbon.DisplayForm. 
  • The command definition location should look like Ribbon.ListForm.Display.Actions.Controls._children. Here, the location denotes the command will be added in actions group under ribbon. You can specify other group names as well.
  • The command action shows the custom function, to be defined. Here, you will get an alert box with the corresponding item ID on click of a button.
The following piece of code adds custom ribbon items to te display form.
  1. function AddDisplayRibbonItem(){    
  2.     $web = $ctx.Site.RootWeb    
  3.     $userActions = $web.UserCustomActions    
  4.     $ctx.Load($userActions)     
  5.     $ctx.ExecuteQuery()     
  6.     
  7.     $newRibbonItem = $userActions.Add()    
  8.     $newRibbonItem.RegistrationId = "100"    
  9.     $newRibbonItem.Title = "Display Form Custom Action"    
  10.     $newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::List    
  11.     $newRibbonItem.Location = "CommandUI.Ribbon.DisplayForm"    
  12.     
  13.     $ribbonUI = '<CommandUIExtension>    
  14.         <CommandUIDefinitions>    
  15.             <CommandUIDefinition Location="Ribbon.ListForm.Display.Actions.Controls._children">    
  16.                  <Button Id="Ribbon.ListForm.Display.Actions.CustomFunction" Alt="Show Alert" Sequence="100"    
  17.                  Command="CustomFunction"    
  18.                  LabelText="Show Item ID"    
  19.                  TemplateAlias="o1"    
  20.                  Image32by32="_layouts/15/images/alertme.png"    
  21.                  Image16by16="_layouts/15/images/alertme.png" />    
  22.             </CommandUIDefinition>    
  23.        </CommandUIDefinitions>    
  24.        <CommandUIHandlers>    
  25.           <CommandUIHandler Command="CustomFunction"    
  26.                CommandAction="javascript:alert(''{ItemId}'');"/>    
  27.           </CommandUIHandlers>    
  28.        </CommandUIExtension >'    
  29.     
  30.     $newRibbonItem.CommandUIExtension = $ribbonUI    
  31.     $newRibbonItem.Update()    
  32.     $ctx.Load($newRibbonItem)    
  33.     $ctx.ExecuteQuery()    
  34. }  
Go to custom list (list template id:100). Select any item and view. The display form page will open with the custom action.

Adding actions to Edit Form:
  • The ribbon custom action location should be CommandUI.Ribbon.EditForm. 
  • The command definition location should look like Ribbon.ListForm.Edit.Actions.Controls._children. Here, the location denotes the command, which will be added in actions group under ribbon. You can specify other group names as well. 
  • The command action shows the custom function to be defined. Here, you will get an alert box with a corresponding item ID on click of a button.
The following piece of code adds custom ribbon items to edit form page.
  1. function AddEditRibbonItem(){  
  2.     $web = $ctx.Site.RootWeb  
  3.     $userActions = $web.UserCustomActions  
  4.     $ctx.Load($userActions)   
  5.     $ctx.ExecuteQuery()   
  6.   
  7.     $newRibbonItem = $userActions.Add()  
  8.     $newRibbonItem.RegistrationId = "100"  
  9.     $newRibbonItem.Title = "Edit Form Custom Action"  
  10.     $newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::List  
  11.     $newRibbonItem.Location = "CommandUI.Ribbon.EditForm"  
  12.   
  13.     $ribbonUI = '<CommandUIExtension>  
  14.         <CommandUIDefinitions>  
  15.             <CommandUIDefinition Location="Ribbon.ListForm.Edit.Actions.Controls._children">  
  16.                  <Button Id="Ribbon.ListForm.Edit.Actions.CustomFunction" Alt="Show Alert" Sequence="100"  
  17.                  Command="CustomFunction"  
  18.                  LabelText="Show Item ID"  
  19.                  TemplateAlias="o1"  
  20.                  Image32by32="_layouts/15/images/alertme.png"  
  21.                  Image16by16="_layouts/15/images/alertme.png" />  
  22.             </CommandUIDefinition>  
  23.        </CommandUIDefinitions>  
  24.        <CommandUIHandlers>  
  25.            <CommandUIHandler Command="CustomFunction"  
  26.                 CommandAction="javascript:alert(''EditForm-{ItemId}'');"/>  
  27.            </CommandUIHandlers>  
  28.        </CommandUIExtension >'  
  29.   
  30.     $newRibbonItem.CommandUIExtension = $ribbonUI  
  31.     $newRibbonItem.Update()  
  32.     $ctx.Load($newRibbonItem)  
  33.     $ctx.ExecuteQuery()  
  34. }  
Go to custom list (list template id:100). Select any item and edit. The edit form page will open with the custom action.


Adding actions to New Form:
  • The ribbon custom action location should be CommandUI.Ribbon.NewForm. 
  • The command definition location should look like Ribbon.ListForm.Edit.Actions.Controls._children(Edit action specified, since edit operation is carried out for the form). Here, the location denotes the command, that will be added in actions group under ribbon. You can specify other group names as well.
  • The command action shows the custom function to be defined. Here, you will get an alert box with a corresponding list ID on click of a button.
The following piece of code adds custom ribbon items to new form page.
  1. function AddNewRibbonItem(){  
  2.     $web = $ctx.Site.RootWeb  
  3.     $userActions = $web.UserCustomActions  
  4.     $ctx.Load($userActions)   
  5.     $ctx.ExecuteQuery()   
  6.   
  7.     $newRibbonItem = $userActions.Add()  
  8.     $newRibbonItem.RegistrationId = "100"  
  9.     $newRibbonItem.Title = "New Form Custom Action"  
  10.     $newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::List  
  11.     $newRibbonItem.Location = "CommandUI.Ribbon.NewForm"  
  12.   
  13.     $ribbonUI = '<CommandUIExtension>  
  14.         <CommandUIDefinitions>  
  15.              <CommandUIDefinition Location="Ribbon.ListForm.Edit.Actions.Controls._children">  
  16.                   <Button Id="Ribbon.ListForm.Display.Actions.NewCustomFunction" Alt="Show Alert" Sequence="100"  
  17.                   Command="NewCustomFunction"  
  18.                   LabelText="Show List ID"  
  19.                   TemplateAlias="o1"  
  20.                         Image32by32="_layouts/15/images/alertme.png"  
  21.                         Image16by16="_layouts/15/images/alertme.png" />  
  22.             </CommandUIDefinition>  
  23.        </CommandUIDefinitions>  
  24.        <CommandUIHandlers>  
  25.             <CommandUIHandler Command="NewCustomFunction"  
  26.                  CommandAction="javascript:alert(''{ListId}'');"/>  
  27.             </CommandUIHandlers>  
  28.       </CommandUIExtension >'  
  29.   
  30.     $newRibbonItem.CommandUIExtension = $ribbonUI  
  31.     $newRibbonItem.Update()  
  32.     $ctx.Load($newRibbonItem)  
  33.     $ctx.ExecuteQuery()  
  34. }  
Go to custom list (list template id:100). Click on the new item. The new form page will open with the custom action. The following image shows the custom action added.

 

The above set of operations (functions) needs to be called, in order to execute it.