Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting UK – HostForLIFE.eu :: Useful CSOM Snippets
1 |
SharePointPnPCoreOnline |
Required imports are:
1 2 3 4 5 |
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)) |
The above code creates authentication with Sharepoint with a valid username and password. It returns a clientcontext object. With the clientcontext object, we will do our required operations in Sharepoint.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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"); } |
1 |
clientContext.Web.CreateList(ListTemplateType.DocumentLibrary, "WithPnpDOC",false); |
The above code will create a new document library in the Sharepoint site. We want to provide a valid listtemplatetype for creating various lists. The types are:
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:
As the heading describes, it checks the library with the foldername “WithPnpDOC1”. If it does not exist, it will create a new folder.
Simply creates the new folder in the Sharepoint document library
1 |
clientContext.Web.CreateContentType("Contenttype name", "description", "Guid ", "Group"); |
The above code will create new contenttype in Sharepoint where the name and guid are unique. In the Guid, starting characters represents the parent content type. The available types are:
- 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
e.g.
- clientContext.Web.CreateContentType(“PNP doc3”, “My Desc”, “0x010100C2D5F149518B119D801AC6C18942554A”, “PNP grp CT”);
Adding fields to Content Type
1 2 3 4 5 6 7 8 |
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); |
The above code will add sitecolumns field to content type PNP ct where guid is the valid site column guid.
1 |
clientContext.Web.SetDefaultContentTypeToList("mynewlist", "0x0100A33D9AD9805788419BDAAC2CCB37509F"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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); |
The above code creates a new datetime site column in your Sharepoint site with the name TestColumn2
1 2 3 4 5 6 7 8 9 10 11 |
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(); |
The above code creates new number field with name Employee IDd in the list “mynewlist” the available types are:
- 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
Print article | This entry was posted by Peter on February 6, 2020 at 7:56 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |