Introduction
In this article, we will learn about some important Csom codes used for SharePoint operations
Nuget Package Required:
- SharePointPnPCoreOnline
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using OfficeDevPnP.Core;
Authenticating with Sharepoint:
- string siteUrl = "Your Site Collection Url";
- string userName = "UserName of Account";
- string password = "Password";
- OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();
- using (var clientContext= authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
Getting Listitems from sharepoint
- string listName = "mynewlist";
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(List);
- if (list != null)
- {
- CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
- ListItemCollection items = list.GetItems(query);
- clientContext.Load(items);
- clientContext.ExecuteQuery();
- foreach (ListItem listItem in items)
- {
- Console.WriteLine(listItem.FieldValues["ID"]);
- }
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
The above code returns items from the sharepoint list, depending upon the caml query provided.
Creating new items in the SharePoint list
- string listName = "mynewlist";
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(List);
- if (list != null)
- {
- ListItemCreationInformation newItem = new ListItemCreationInformation();
- ListItem listItem = list.AddItem(newItem);
- listItem["Title"] = "Madhan";
- listItem.Update();
- clientContext.ExecuteQuery();
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
The above code will insert a new item in the SharePoint list (i.e. create a new id and Title value as Madhan)
Updating Listitem in sharepoint
- string listName = "mynewlist";
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(List);
- if (list != null)
- {
- ListItem oListItem = list.GetItemById(5);
- clientContext.Load(oListItem);
- clientContext.ExecuteQuery();
- // clientContext.ExecuteQuery();
- if (oListItem.FieldValues.Count >0) {
- oListItem["Title"] = "My Updated Title.";
- oListItem.Update();
- clientContext.ExecuteQueryRetry(); }
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
Basically updating and deleting are processed based on ID, the above code will update the value of Title field to "My Updated Title" for Id "5" in the SharePoint list mynewlist
Deleting a ListItem in Sharepoint
- string listName = "mynewlist";
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(List);
- ListItem oListItem = list.GetItemById(5);
- clientContext.Load(oListItem);
- clientContext.ExecuteQuery();
- if (oListItem.FieldValues.Count > 0)
- {
- oListItem.DeleteObject();
- clientContext.ExecuteQuery();
- }
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
As I said earlier, deleting an item is based on ID. The above code will delete the listitem corresponding to the ID "5"
Creating a New List
- clientContext.Web.CreateList(ListTemplateType.DocumentLibrary, "WithPnpDOC",false);
| GenericList | 100 | 0 | A basic list which can be adapted for multiple purposes. |
| DocumentLibrary | 101 | 1 | Contains a list of documents and other files. |
| Survey | 102 | 4 | Fields on a survey list represent questions that are asked of survey participants. Items in a list represent a set of responses to a particular survey. |
| Links | 103 | 0 | Contains a list of hyperlinks and their descriptions. |
| Announcements | 104 | 0 | Contains a set of simple announcements. |
| Contacts | 105 | 0 | Contains a list of contacts used for tracking people in a site. |
| Events | 106 | 0 | Contains a list of single and recurring events. An events list typically has special views for displaying events on a calendar. |
| Tasks | 107 | 0 | Contains a list of items that represent completed and pending work items. |
| DiscussionBoard | 108 | 0 | Contains discussions topics and their replies. |
| PictureLibrary | 109 | 1 | Contains a library adapted for storing and viewing digital pictures. |
| DataSources | 110 | 1 | Contains data connection description files. |
| XmlForm | 115 | 1 | Contains XML documents. An XML form library can also contain templates for displaying and editing XML files via forms, as well as rules for specifying how XML data is converted to and from list items. |
| NoCodeWorkflows | 117 | 1 | Contains additional workflow definitions that describe new processes that can be used within lists. These workflow definitions do not contain advanced code-based extensions. |
| WorkflowProcess | 118 | 0 | Contains a list used to support execution of custom workflow process actions. |
| WebPageLibrary | 119 | 1 | Contains a set of editable Web pages. |
| CustomGrid | 120 | 0 | Contains a set of list items with a grid-editing view. |
| WorkflowHistory | 140 | 0 | Contains a set of history items for instances of workflows. |
| GanttTasks | 150 | 0 | Contains a list of tasks with specialized Gantt views of task data. |
| IssuesTracking | 1100 | 5 | Contains a list of items used to track issues. |
Checking if the folder exists. If not, then creating a new folder:
- string listName = "mynewlist";
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(List);
- var folder = list1.RootFolder.EnsureFolder("WithPnpDOC1");
Creating a new Folder without checking existing
- string listName = "mynewlist";
- List list = clientContext.Web.Lists.GetByTitle(listName);
- clientContext.Load(List);
- var folder = list.RootFolder.CreateFolder("WithPnpDOC");
Creating Contenttype
- clientContext.Web.CreateContentType("Contenttype name", "description", "Guid ", "Group");
- System 0x
- Item 0x01
- Document 0x0101
- Event 0x0102
- Issue 0x0103
- Announcement 0x0104
- Link 0x0105
- Contact 0x0106
- Message 0x0107
- Task 0x0108
- Workflow History 0x0109
- Post 0x0110
- Comment 0x0111
- Folder x0120
- clientContext.Web.CreateContentType("PNP doc3", "My Desc", "0x010100C2D5F149518B119D801AC6C18942554A", "PNP grp CT");
- string fieldIdStr = "5b0a8635-49a5-469f-8d42-1b92e8d4e17b";
- string contentTypeName = "PNP Ct";
- bool isRequired = false;
- bool isHidden = false;
- // Field As Guid
- Guid fieldId = new Guid(fieldIdStr);
- // Adds Field to Content Type using Name
- clientContext.Web.AddFieldToContentTypeByName(contentTypeName, fieldId, isRequired, isHidden);
Setting default Content type to list
- clientContext.Web.SetDefaultContentTypeToList("mynewlist", "0x0100A33D9AD9805788419BDAAC2CCB37509F");
Creating Fields and SiteColumn
- string displayName = "TestColumn2";
- string internalName = "TestColumn2";
- string groupName = "TestPnPGroup";
- string fieldId = "4FDD339E-6B98-4A11-B1AC-9C6B8C37AE2E";
- FieldType fieldType = FieldType.DateTime;
- OfficeDevPnP.Core.Entities.FieldCreationInformation newFieldInfo = new OfficeDevPnP.Core.Entities.FieldCreationInformation(fieldType)
- {
- DisplayName = displayName,
- InternalName = internalName,
- Id = new Guid(fieldId),
- Group = groupName
- };
- // Creates new Field
- Field newField = mycleint.Web.CreateField(newFieldInfo);
-
string listName = "mynewlist";List list = clientContext.Web.Lists.GetByTitle(listName);clientContext.Load(List);
- OfficeDevPnP.Core.Entities.FieldCreationInformation fldEmpID = new OfficeDevPnP.Core.Entities.FieldCreationInformation(FieldType.Number);
- fldEmpID.DisplayName = "Employee IDd";
- fldEmpID.InternalName = "EmpIDd";
- fldEmpID.AddToDefaultView = true;
- fldEmpID.Id = Guid.NewGuid();
- list.CreateField(fldEmpID);
- list.Update();
- mycleint.ExecuteQueryRetry();
- Invalid = 0,
- Integer = 1,
- Text = 2,
- Note = 3,
- DateTime = 4,
- Counter = 5,
- Choice = 6,
- Lookup = 7,
- Boolean = 8,
- Number = 9,
- Currency = 10,
- URL = 11,
- Computed = 12,
- Threading = 13,
- Guid = 14,
- MultiChoice = 15,
- GridChoice = 16,
- Calculated = 17,
- File = 18,
- Attachments = 19,
- User = 20,
- Recurrence = 21,
- CrossProjectLink = 22,
- ModStat = 23,
- Error = 24,
- ContentTypeId = 25,
- PageSeparator = 26,
- ThreadIndex = 27,
- WorkflowStatus = 28,
- AllDayEvent = 29,
- WorkflowEventType = 30,
- Geolocation = 31,
- OutcomeChoice = 32,
- Location = 33,
- Thumbnail = 34,
- MaxItems = 35
Conclusion
We have learned some crucial operations in SharePoint using PnpCore CSOM. Hope this helps somone. Happy Coding! :)

Join the conversation! Your thoughts help the community grow.