share point 2013 tips and tricks
Post on 16-Oct-2015
510 Views
Preview:
DESCRIPTION
TRANSCRIPT
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 2
I would like to dedicate this eBook to my parents and my friends.
- Vijai Anand. R
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 3
TABLE OF CONTENTS
1 SharePoint 2013 .Net Client Side Object Model: An Overview .......................................................... 11
2 Create a console application using Visual Studio 2012 ...................................................................... 14
3 Perform SharePoint list tasks using CSOM.......................................................................................... 19
3.1 How to get all the lists from the website .................................................................................... 19
3.2 How to create a new list in the website ..................................................................................... 20
3.3 How to delete a list from the website ........................................................................................ 22
3.4 How to update a list in the website ............................................................................................ 23
3.5 How to enable folder creation for the list in the website .......................................................... 24
3.6 How to disable attachments to list items in the website ........................................................... 26
3.7 How to display the list in the quick launch bar ........................................................................... 27
3.8 How to enable versioning for the list .......................................................................................... 29
3.9 How to enable minor versions for the document library ........................................................... 31
3.10 How to enable force check out for the document library .......................................................... 33
3.11 How to enable content approval for the list ............................................................................... 35
3.12 How to specify the permission required to view minor versions and drafts within the list ....... 36
3.13 How to get all the list templates available for creating lists ....................................................... 38
4 Perform SharePoint website tasks using CSOM.................................................................................. 41
4.1 How to get the properties of a website ...................................................................................... 41
4.2 How to update the properties of a website ................................................................................ 42
4.3 How to get only specific properties of a website ....................................................................... 43
4.4 How to delete a website ............................................................................................................. 45
4.5 How to get all available web templates from website ............................................................... 46
4.6 How to get all the active features from website ........................................................................ 48
5 Perform SharePoint list item tasks using CSOM ................................................................................. 50
5.1 How to get all the items from the list ......................................................................................... 50
5.2 How to create a new item in the list ........................................................................................... 51
5.3 How to update an item in the list ............................................................................................... 53
5.4 How to delete an item in the list ................................................................................................ 54
5.5 How to get the items from a list folder ....................................................................................... 55
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 4
5.6 How to get the limited number of items from the list ............................................................... 57
5.7 How to get all the attachments for the list item ........................................................................ 58
5.8 How to delete an attachment for the list item ........................................................................... 60
6 Perform SharePoint content type tasks using CSOM ......................................................................... 62
6.1 How to get all the content types from the website .................................................................... 62
6.2 How to create a site content type .............................................................................................. 63
6.3 How to delete the site content type ........................................................................................... 66
6.4 How to set the site content type read only ................................................................................ 67
6.5 How to get all the content types from the list ............................................................................ 68
6.6 How to delete the content type from the list ............................................................................. 70
6.7 How to add existing content type to the list .............................................................................. 71
7 Perform SharePoint field tasks using CSOM ....................................................................................... 73
7.1 How to get all the fields from the list ......................................................................................... 73
7.2 How to update a specific field available in the list ..................................................................... 74
7.3 How to add a field in the list ....................................................................................................... 75
7.4 How to add an existing field to the list ....................................................................................... 77
7.5 How to delete a field from the list .............................................................................................. 78
7.6 How to set the default value for the list field ............................................................................. 79
7.7 How to get the calculated field formula ..................................................................................... 80
7.8 How to set the formula for the calculated field.......................................................................... 82
8 Perform SharePoint list view tasks using CSOM ................................................................................. 84
8.1 How to get all the views for the list ............................................................................................ 84
8.2 How to create a new list view ..................................................................................................... 85
8.3 How to get all the fields available in the list view....................................................................... 86
8.4 How to set the default view in the list ........................................................................................ 88
8.5 How to add a field to the list view .............................................................................................. 89
8.6 How to delete a field from the list view...................................................................................... 90
8.7 How to delete a list view ............................................................................................................. 91
9 Perform SharePoint folder tasks using CSOM..................................................................................... 94
9.1 How to get all the top level folders from the website ................................................................ 94
9.2 How to get all the top level folders from the list ........................................................................ 95
9.3 How to get the subfolders from the list ...................................................................................... 97
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 5
9.4 How to delete a folder from the list ........................................................................................... 98
9.5 How to create a new folder in the document library ................................................................. 99
9.6 How to get the number of items inside the folder ................................................................... 100
10 Perform SharePoint file tasks using CSOM ................................................................................... 103
10.1 How to get the major version of the file ................................................................................... 103
10.2 How to get the minor version of the file................................................................................... 104
10.3 How to check out the file in the document library ................................................................... 106
10.4 How to get the user login name who has checked out the file ................................................ 107
10.5 How to get the user login name who added the file ................................................................ 108
10.6 How to get the check out type associated with the file ........................................................... 110
10.7 How to check in the file ............................................................................................................ 111
10.8 How to get the check in comment of the file ........................................................................... 113
10.9 How to unpublish the major version of the file ........................................................................ 114
10.10 How to discard check out of the file ..................................................................................... 115
10.11 How to delete the file from the document library ............................................................... 117
11 Perform SharePoint file version tasks using CSOM ...................................................................... 119
11.1 How to get all the versions for the file ..................................................................................... 119
11.2 How to get the file version for the document by version Id ..................................................... 121
11.3 How to delete a file version by version ID for the document ................................................... 122
11.4 How to delete a file version by version label for the document .............................................. 124
11.5 How to restore a specific file version for the document .......................................................... 126
11.6 How to check if the file version is a current version for the document ................................... 127
11.7 How to delete all the file versions for the document ............................................................... 129
12 Perform SharePoint group tasks using CSOM ............................................................................... 131
12.1 How to get all the site groups ................................................................................................... 131
12.2 How to create a new site group ................................................................................................ 132
12.3 How to set the user as owner for the site group ...................................................................... 133
12.4 How to set the group as owner for the site group ................................................................... 135
12.5 How to get all the users from the site group ............................................................................ 137
12.6 How to add a user to the site group ......................................................................................... 138
12.7 How to remove a user from the site group .............................................................................. 140
12.8 How to delete a site group ........................................................................................................ 141
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 6
13 Perform SharePoint role tasks using CSOM .................................................................................. 143
13.1 How to get all the permission levels from the website ............................................................ 143
13.2 How to create a permission level in the website ...................................................................... 144
13.3 How to update the permission level in the website ................................................................. 146
13.4 How to remove the permissions from the permission level..................................................... 149
13.5 How to delete the permission level from the website ............................................................. 150
13.6 How to assign the role to a specific group ................................................................................ 152
13.7 How to assign the role to a specific user .................................................................................. 153
14 Perform SharePoint Taxonomy related tasks using CSOM ........................................................... 156
14.1 How to get all the Term Stores for the provided site ............................................................... 156
14.2 How to get the Default site collection Termstore .................................................................... 157
14.3 How to get the default keyword termstore .............................................................................. 159
14.4 How to get all the groups for the termstore ............................................................................. 160
14.5 How to create a new group for the term store ........................................................................ 161
14.6 How to delete the group from the term store .......................................................................... 163
14.7 How to get all the termsets for the taxonomy group ............................................................... 164
14.8 How to create a term set for the specified group .................................................................... 166
14.9 How to delete the term set from the specified group .............................................................. 167
14.10 How to get all the terms for the termset .............................................................................. 169
14.11 How to create a new term for the termset........................................................................... 170
14.12 How to delete the term from the term set ........................................................................... 172
14.13 How to create a copy of the term within the termset .......................................................... 173
14.14 How to move the specified term to different termset ......................................................... 175
14.15 How to deprecate the specified term ................................................................................... 176
14.16 How to get all the labels for specified term .......................................................................... 178
14.17 How to create a new label for specified term ...................................................................... 180
14.18 How to delete the label for specified term ........................................................................... 182
15 Perform SharePoint User Profile tasks using CSOM ..................................................................... 184
15.1 How to get a particular users properties ................................................................................. 184
15.2 How to get the current users properties ................................................................................. 185
15.3 How to get the specific user profile property for a user .......................................................... 186
16 Perform SharePoint social tasks using CSOM ............................................................................... 189
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 7
16.1 How to check the specified user is following the target user ................................................... 189
16.2 How to get the count of people the user is following .............................................................. 190
16.3 How to get the count of documents followed by the user ....................................................... 192
16.4 How to get the count of sites followed by the user.................................................................. 193
16.5 How to get the count of actors followed by the user ............................................................... 195
16.6 How to get all the followers for the user .................................................................................. 196
16.7 How to get all the people followed by the user ....................................................................... 198
16.8 How to get all the documents followed by the user................................................................. 200
16.9 How to get all the sites followed by the user ........................................................................... 201
16.10 How to follow the target user ............................................................................................... 203
16.11 How to stop following the target user .................................................................................. 205
16.12 How to follow the site ........................................................................................................... 206
16.13 How to stop following the site .............................................................................................. 209
16.14 How to follow the document ................................................................................................ 210
16.15 How to stop following the document ................................................................................... 212
Summary: .................................................................................................................................................. 215
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 8
About the Author:
Vijai Anand has been working in the IT industry for over 4 years. He holds a Bachelor's degree in Electronics and
Communication Engineering. He works as a SharePoint Developer at Cognizant Technology Solutions,
Bangalore. Vijai has worked on Microsoft Office SharePoint Server 2007, Microsoft SharePoint 2010 and
Microsoft SharePoint 2013.
Vijai is a frequent contributor on C# Corner (www.c-sharpcorner.com). He has authored around 400 articles and
360 blogs on www.csharpcorner.com regarding SharePoint 2013, SharePoint 2010, SharePoint Workspace,
SharePoint Designer 2010, Powershell, C # and Silverlight. He currently holds Microsoft Most Valuable
Professional (MVP) and Mindcracker Most Valuable Professional awards for his SharePoint Server
contributions.
He has authored the following eBooks:
Getting Started with Managed Metadata Service in SharePoint 2010 published in CSHARPCORNER.com
Business Data Connectivity Services - Step by Step tutorial published in ITFUNDA.com
He has accomplished the following Microsoft Certifications:
Microsoft SharePoint 2010, Application Development
Microsoft SharePoint 2010, Designing and Developing Microsoft SharePoint 2010 Applications.
Microsoft Office SharePoint Server 2007, Application Development
Microsoft Office SharePoint Server 2007, Configuration
Who can read this book?
SharePoint Developers who have some basic knowledge about SharePoint 2010 or 2013 will find this book
helpful for understanding and working with the .Net Client Side Object Model. This book is mainly focused on
beginner functionality and code samples to perform basic opertaions using the .Net Client Side Object Model.
For advanced developers, sections 14, 15 and 16 will be more useful that explain the operations that can be
performed by the new assemblies added in the SharePoint 2013 Client Side Object Model. With respect to the
code samples in this book, you should be familiar with SharePoint Object Model and out-of-the-box features.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 9
Acknowledgment:
I am really thankful to each and everyone who motivated me to write articles and publish my third eBook.
I would like to express my special thanks to Mahesh Chand (Microsoft MVP, Founder of Mindcracker
Networks) and to the entire CSharpcorner team for motivating me to publish my third eBook. I am really
thankful to Sam Hobbs (C# Corner Editor) for reviewing and editing my eBook.
I would like to thank Satveer Khurpa (SharePoint Lead Architect) who did technical review for this
eBook.Thanks to all the reviewers for reviewing my eBook.
I would like to express my special thanks to M.Kavya who designed the cover page of this eBook.
I would like to express my thanks to all my colleagues and Architects who supported me to write this book.
Thanks to all my friends who helped me to publish this eBook.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 10
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 11
1 SharePoint 2013 .Net Client Side Object Model: An Overview
The
Client Side Object Model (CSOM) was first introduced in SharePoint 2010. The Client Side Object Model is
mainly used to build client applications and enable us to access SharePoint Sites that are hosted outside
without using webservices. Prior to the CSOM, developers had only a few choices to build client applications.
However, this has changed now. With the introduction of CSOM, developers now have more choices and will be
able to access the core SharePoint functionalites to a wide extent. In SharePoint 2010, the CSOM exposed the
core SharePoint functionalites only whereas in SharePoint 2013, the Microsoft SharePoint team has added a few
more assemblies. Now we are able to access the Service Applications using the Client Side Object Model.
There are three programming models used for the Client Side Object Model; they are:
.Net Client Side Object Model.
Javascript Object Model.
Silverlight Object Model.
Assemblies for the above models are located in the SharePoint file system. The .Net Client Side Object Model is
located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI. The Silverlight
Object Model is located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15
\TEMPLATE\LAYOUTS\ClientBin. ECMAScripts are located at C:\Program Files\Common Files\Microsoft
Shared\Web Server Extensions\15 \TEMPLATE\LAYOUTS.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 12
Figure1.1: Client Side Object Model Architecture
How does it work? If you use the Client Side API to perform a specific task then the SharePoint .Net client
object model bundles up these uses of the API into XML and send them to the server. The server receives this
request, and makes appropriate calls into the object model on the server, collects the responses, forms them
into JavaScript Object Notation (JSON), and sends that JSON back to the SharePoint .Net client object model.
The client object model parses the JSON and presents the results to the application.
Please find the below table to know few client-side classes and server-side equivalents
Client-side classes and server-side equivalents
Client Server
ClientContext SPContext
Site SPSite
Web SPWeb
List SPList
ListItem SPListItem
Field SPField
To build a basic operation using the .Net Client Side Object Model there are some very essential classes and
methods that need to be used that are explained below.
ClientContext class: the ClientContext class gets the current context by passing the site URL, which inherits
from the ClientContextRuntime class.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 13
Load() method: the Load() method does not actually load anything, only when the ExecuteQuery() method is
called does it provide notification that these are the property values that should be loaded for the object. In the
Load() method lamda expressions can be used to specify which property should be loaded instead of loading
all the properties.
ExecuteQuery() method: the ExecuteQuery() method sends the request to the server. There is no network
traffic until the application calls this method. Until the ExecuteQuery() method is called only the requests are
registered by the application.
In the later sections you will see the code samples to perform the basic operations using the SharePoint 2013
Client Side Object Model.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 14
2 Create a console application using Visual Studio 2012
In this section you will see how to create a console application using Visual Studio 2012 which will be used to
explain all the examples for the .Net Client Side Object Model.
Steps Involved:
1. Open Visual Studio 2012 (Run as administrator).
2. Go to "File" => "New" => "Project...".
Figure2.1: Create a new project in Visual Studio 2012
3. Select Console Application in the Visual C# node from the installed templates.
4. Enter the Name as CSOMCodeSamples and click on OK.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 15
Figure2.2: Select the console application template
5. Right-click on the project and then click on Properties. Click on the Build tab and ensure that the
following options are selected properly.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 16
Figure2.3: Project properties
6. In the Solution Explorer, right-click on the References folder and then click on Add Reference.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 17
Figure2.4: Add reference
7. Add the following assemblies from hive 15 (C:\Program Files\Common Files\microsoft shared\Web
Server Extensions\15\ISAPI).
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 18
Figure2.5: Add the references
Microsoft.SharePoint.Client.Taxonomy.dll should be added to perform taxonomy related tasks and and
Microsoft.SharePoint.Client.UserProfiles.dll shoul be added to perform user profile related tasks.
Thus in this section you have seen how to create a console application using Visual Studio 2012.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 19
3 Perform SharePoint list tasks using CSOM
In this section you will see how to perform list related tasks using the SharePoint 2013 .Net Client Side Object
Model.
3.1 How to get all the lists from the website
In this example you will see how to get all the lists from the website using the .Net Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list collection for the web
ListCollection listColl = web.Lists;
// Retrieve the list collection properties
clientContext.Load(listColl);
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 20
// Execute the query to the server.
clientContext.ExecuteQuery();
// Loop through all the list
foreach (List list in listColl)
{
// Display the list title and ID
Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);
}
Console.ReadLine();
}
}
}
Output
Figure3.1.1: Get all the lists from the list
3.2 How to create a new list in the website
In this example you will see how to create a new list in the website using the .Net Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 21
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Specifies the properties of the new custom list
ListCreationInformation creationInfo = new ListCreationInformation();
creationInfo.Title = "Custom List";
creationInfo.Description = "Custom list created using CSOM";
creationInfo.TemplateType = (int)ListTemplateType.GenericList;
// Create a new custom list
List newList=clientContext.Web.Lists.Add(creationInfo);
// Retrieve the custom list properties
clientContext.Load(newList);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Display the custom list Title property
Console.WriteLine(newList.Title);
Console.ReadLine();
}
}
}
Output
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 22
Figure3.2.1: Create a new list
3.3 How to delete a list from the website
In this example you will see how to delete a list from the website using the Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by Title
List list = web.Lists.GetByTitle("Custom");
// Delete the list object
list.DeleteObject();
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 23
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
The Custom list is deleted successfully.
3.4 How to update a list in the website
In this example you will see how to update a list in the website using the .Net Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 24
// Update the description for the custom list
list.Description = "Custom list description updated using CSOM";
// Update the list
list.Update();
// Retrieve the list properties
clientContext.Load(list);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Display the list title and description
Console.WriteLine("List Title: " + list.Title + "; Description: " +
list.Description);
Console.ReadLine();
}
}
}
Output
Figure3.4.1: Update the list
3.5 How to enable folder creation for the list in the website
In this example you will see how to enable folder creation for the list in the website using the .Net Client Side
Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 25
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
// Enable folder creation for the custom list
list.EnableFolderCreation = true;
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Custom List link in the quick launch bar. Click on the List Settings
button in the ribbon interface. Click on the Advanced Settings link which is available under the General
Settings section. You will be able to see that the folder creation for the list has enabled successfully.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 26
Figure3.5.1: Enable folder creation for the list
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.list.enablefoldercreation.aspx
3.6 How to disable attachments to list items in the website
In this example you will see how to disable attachments to list items in the website using the .Net Client Side
Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
// Disable attachments to list items
list.EnableAttachments = false;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 27
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Custom List link in the quick launch bar. Click on the List Settings
button in the ribbon interface. Click on the Advance Settings link which is available under the General
Settings section. You will be able to see thart the attachments to list items are disabled successfully.
Figure3.6.1: Disable attachments to list items
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.list.enableattachments.aspx
3.7 How to display the list in the quick launch bar
In this example you will see how to display the list in the quick launch bar using the .Net Client Side Object
Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 28
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
// Display the list in the quick launch bar
list.OnQuickLaunch = true;
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Custom List link in the quick launch bar. Click on the List Settings
button in the ribbon interface. Click on the List name, description and navigation link which is available under
the General Settings section. You will be able to see that the option to display the list in the quick launch has
been enabled successfully.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 29
Figure3.7.1: Display the list on the quick launch
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.onquicklaunch.aspx
3.8 How to enable versioning for the list
In this example you will see how to enable versioning for the list using the .Net Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
// Enable versioning for the list
list.EnableVersioning = true;
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Custom List link in the quick launch bar. Click on the List Settings
button in the ribbon interface. Click on the Version Settings link which is available under the Settings section.
You will be able to see that versioning the settings for the list has been enabled successfully.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 31
Figure3.8.1: Enable versioning for the list
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.enableversioning.aspx
3.9 How to enable minor versions for the document library
In this example you will see how to enable minor versions for the document library using the .Net Client Side
Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 32
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Documents");
// Enable minor versions for the document library
list.EnableMinorVersions = true;
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Documents link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link which is available under the
Settings section. You will be able to see that the minor versions for the document library has been enabled
successfully.
Figure3.9.1: Enable minor versions for the list
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 33
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.enableminorversions.aspx
Note
If you try to run the same code for a custom list then you will get the error as shon in Figure 3.9.2. This is
because there is no option to enable minor versions in the custom list.
Figure3.9.2: Error message
3.10 How to enable force check out for the document library
In this example you will see how to enable forced check out for the document library using the .Net Client Side
Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 34
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Documents");
// Enable force check out for document library
list.ForceCheckout = true;
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Documents link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link which is available under the
Settings section. You will be able to see that the forced check out for the document library has been enabled
successfully.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 35
Figure3.10.1: Enable force check out for the list
Reference
http://msdn.microsoft.com/en-
us/library/microsoft.sharepoint.client.list.forcecheckout%28v=office.14%29.aspx
3.11 How to enable content approval for the list
In this example you will see how to enable content approval for the list using the .Net Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
// Enable content approval for the list
list.EnableModeration = true;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 36
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Custom List link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link which is available under the
General Settings section.
Figure3.11.1: Enable content approval for the list
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.draftversionvisibility.aspx
3.12 How to specify the permission required to view minor versions and
drafts within the list
In this example you will see how to specify the permission required viewing minor versions and drafts within the
list using the .Net Client Side Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list by title
List list = web.Lists.GetByTitle("Custom List");
// Specify the permission required to view minor versions and drafts within
the list.
// You can select the Draft Visibility Type as either Author, Approve or Read
list.DraftVersionVisibility = DraftVisibilityType.Approver;
// Update the list
list.Update();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
Navigate to the SharePoint site. Click on the Custom List link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link which is available under General
Settings section.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 38
Figure3.13.1: Specify which users should be able to view drafts in this list
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.draftversionvisibility.aspx
3.13 How to get all the list templates available for creating lists
In this example you will see how to get all the list templates available for creating lists using the .Net Client Side
Object Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 39
// Get the SharePoint web
Web web = clientContext.Web;
// Get all the list templates available for creating lists on the site
ListTemplateCollection templateColl = web.ListTemplates;
clientContext.Load(templateColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the list templates
foreach (ListTemplate template in templateColl)
{
// Display the list template name
Console.WriteLine("Template Name: "+template.Name);
}
Console.ReadLine();
}
}
}
Output
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 40
Figure3.13.1: Get all the available list templates
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 41
4 Perform SharePoint website tasks using CSOM
In this section you will see how to perform website related tasks using the SharePoint 2013 .Net Client Side
Object Model.
4.1 How to get the properties of a website
In this example you will see how to retrieve the website properties using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClienContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM");
// Get the SharePoint web
Web web = clientContext.Web;
// Load the Web properties
clientContext.Load(web);
// Execute the query to the server.
clientContext.ExecuteQuery();
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 42
// Web properties - Display the Title and URL for the web
Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);
Console.ReadLine();
}
}
}
Output
Figur4.1.1: Properties of the website
4.2 How to update the properties of a website
In this example you will see how to update the website properties using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClienContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 43
ClientContext("http://servername/sites/CSOM");
// Get the SharePoint web
Web web = clientContext.Web;
// Update the web Title and Description properties
web.Title = "CSOM Updated";
web.Description = "Updated the web title using CSOM";
web.Update();
// Load the Web properties
clientContext.Load(web);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Web properties - Display the Title and URL for the web
Console.WriteLine("Title: " + web.Title + "; Description: " +
web.Description);
Console.ReadLine();
}
}
}
Output
Figure4.2.1: Update the properties of the website
4.3 How to get only specific properties of a website
In this example you will see how to get specific website properties using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClienContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM");
// Get the SharePoint web
Web web = clientContext.Web;
// Retrieve only the Title property
clientContext.Load(web,w=>w.Title);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Web property - Display the Title
Console.WriteLine("Title: " + web.Title);
Console.ReadLine();
}
}
}
Output
Figure4.3.1: Get the specific property of the website
Note
If you try to retrieve any other property then you will get the following error:
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 45
Figure4.3.2: Error Message
4.4 How to delete a website
In this example you will see how to delete a website using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClienContext - Get the context for the SharePoint Site
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 46
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/New Web/");
// Delete the web object
clientContext.Web.DeleteObject();
// Execute the query to the server.
clientContext.ExecuteQuery();
}
}
}
Output
The Website is deleted successfully.
4.5 How to get all available web templates from website
In this example you will see how to get all the available web templates using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 47
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get all the site templates available for the site
WebTemplateCollection templateColl = web.GetAvailableWebTemplates(1033,
false);
clientContext.Load(templateColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the site templates available for the site
foreach (WebTemplate template in templateColl)
{
// Display the site template Name and Title property
Console.WriteLine("Template Name: " + template.Name + " ; Title: " +
template.Title);
}
Console.ReadLine();
}
}
}
Output
Figure4.5.1: Get all available web templates
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 48
4.6 How to get all the active features from website
In this example you will see how to get all the active web features using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get all the active features from the site
FeatureCollection featureColl = web.Features;
clientContext.Load(featureColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the active features
foreach (Feature feature in featureColl)
{
// Display the feature ID
Console.WriteLine("Feature ID: " + feature.DefinitionId);
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 49
}
Console.ReadLine();
}
}
}
Output
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 50
Figure4.6.1: Get all the active features
5 Perform SharePoint list item tasks using CSOM
In this section you will see how to perform list item related tasks using the SharePoint 2013 .Net Client Side
Object Model.
5.1 How to get all the items from the list
In this example you will see how to get all the items from the list using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// CamlQuery to retrieve the items from the custom list
CamlQuery query = CamlQuery.CreateAllItemsQuery();
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 51
// Get all the items from the list
ListItemCollection itemColl = list.GetItems(query);
clientContext.Load(itemColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the items
foreach (ListItem item in itemColl)
{
// Display the item title field value
Console.WriteLine(item["Title"].ToString());
}
Console.ReadLine();
}
}
}
Output
Figure 5.1.1: Get all the list items
5.2 How to create a new item in the list
In this example you will see how to create a new item in the list using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 52
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Create a new item
ListItemCreationInformation creationInfo = new ListItemCreationInformation();
ListItem item = list.AddItem(creationInfo);
// Set the title value for the new item
item["Title"] = "New Item";
// Update the item
item.Update();
clientContext.Load(item);
// Execute the query to the server
clientContext.ExecuteQuery();
// Display the new item title field value
Console.WriteLine(item["Title"].ToString());
Console.ReadLine();
}
}
}
Output
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 53
Figure5.2.1: Create a new list item
5.3 How to update an item in the list
In this example you will see how to update an item in the list using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Get the list item by ID
ListItem item = list.GetItemById(1);
// Update the list item
item["TextColumn"] = "Item Updated";
item.Update();
// Retrieve the item properties
clientContext.Load(item);
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 54
// Execute the query to the server
clientContext.ExecuteQuery();
// Display the item title field value
Console.WriteLine(item["TextColumn"].ToString());
Console.ReadLine();
}
}
}
Output
Figure5.3.1: Update an item
Output
Here I am the item by ID 1 which exists in my list. Make sure you item ID exists in the list.
5.4 How to delete an item in the list
In this example you will see how to delete an item in the list using the .Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 55
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Get the list item by ID
ListItem item = list.GetItemById(1);
// Update the list item
item.DeleteObject();
// Execute the query to the server
clientContext.ExecuteQuery();
}
}
}
Output
The list item is deleted successfully.
5.5 How to get the items from a list folder
In this example you will see how to get the items from the specified server relative URL of a list folder using the
.Net Client Side Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 56
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Viewfields
string[] viewFields = { "Title" };
// CamlQuery to retrieve the items from the custom list
CamlQuery query = new CamlQuery();
// Specify the server relative URL of a list folder from which results will
be returned.
query.FolderServerRelativeUrl = "/sites/CSOM/Lists/Custom List/FolderA";
// Get the items from the list
ListItemCollection itemColl = list.GetItems(query);
clientContext.Load(itemColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the items
foreach (ListItem item in itemColl)
{
// Display the item title field value
Console.WriteLine(item["Title"].ToString());
}
Console.ReadLine();
}
}
}
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 57
Output
Figure 5.5.1: Get items from the list folder
5.6 How to get the limited number of items from the list
In this example you will see how to get the limited number of items from the list using the .Net Client Side
Object Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 58
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Viewfields
string[] viewFields = { "Title" };
// CamlQuery to retrieve the items from the custom list
// Use row limit and viewfields - To fetch only 2 items and Title field value
CamlQuery query = CamlQuery.CreateAllItemsQuery(2,viewFields);
// Get all the items from the list
ListItemCollection itemColl = list.GetItems(query);
clientContext.Load(itemColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the items
foreach (ListItem item in itemColl)
{
// Display the item title field value
Console.WriteLine(item["Title"].ToString());
}
Console.ReadLine();
}
}
}
Output
Figure 5.6.1: Get the limited number of items
5.7 How to get all the attachments for the list item
In this example you will see how to get all the attachments for the list item using the .Net Client Side Object
Model.
Build the project using the following:
a. Create a console application.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 59
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Get the list item by ID
ListItem item = list.GetItemById(9);
// Get all the attachments for the list item
AttachmentCollection attachColl = item.AttachmentFiles;
clientContext.Load(attachColl);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the attachments for an list item
foreach (Attachment attachment in attachColl)
{
// Display the file name of the attachment
Console.WriteLine(attachment.FileName);
}
Console.ReadLine();
}
}
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 60
}
Output
Figure 5.7.1: Get all the attachments
5.8 How to delete an attachment for the list item
In this example you will see how to get all the attachments for the list item using the .Net Client Side Object
Model.
Build the project using the following:
a. Create a console application.
b. Replace Program.cs with the below source code below.
c. Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 61
// Get the list by Title
List list = web.Lists.GetByTitle("Custom List");
// Get the list item by ID
ListItem item = list.GetItemById(9);
// Get the attachment by file name
Attachment attach = item.AttachmentFiles.GetByFileName("Doc1.docx");
// Delete the attachment
attach.DeleteObject();
// Execute the query to the server
clientContext.ExecuteQuery();
}
}
}
Output
The attachment is deleted successfully for the list item.
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 62
6 Perform SharePoint content type tasks using CSOM
In this section you will see how to perform content type related tasks using the SharePoint 2013 .Net Client Side
Object Model.
6.1 How to get all the content types from the website
In this example you will see how to get all the content types from the website using the .Net Client Side Object
Model.
Build the project using the following:
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get all the content types from the website
ContentTypeCollection ctColl = web.ContentTypes;
// Retrieve all content types from the website
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 63
clientContext.Load(ctColl,
coll=>coll.Include(contentType=>contentType.Name));
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the content types
foreach (ContentType ct in ctColl)
{
// Display the content type name
Console.WriteLine(ct.Name);
}
Console.ReadLine();
}
}
}
Output
Figure6.1.1: Get all the content types from the website
6.2 How to create a site content type
In this example you will see how to create a new site content type using the .Net Client Side Object Model.
Build the project using the following:
-
Code Samples SharePoint 2013 .Net Client Side Object Model
Vijai Anand. R Page 64
a) Create a console application.
b) Replace Program.cs with the below source code below.
c) Hit F5.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace CSOMCodeSamples
{
class Program
{
static void Main(string[] args)
{
// ClientContext - Get the context for the SharePoint Site
// SharePoint site URL - http://servername/sites/CSOM
ClientContext clientContext = new
ClientContext("http://servername/sites/CSOM/");
// Get the SharePoint web
Web web = clientContext.Web;
// Get all the content types from the website
ContentTypeCollection ctColl = web.ContentTypes;
// Get the parent content type - Item (0x01)
ContentType parentCT = web.ContentTypes.Ge
top related