We are continuing from the last post where we created some examples using Managed COM. Please read post from Part -1 to Part - 4 to see all the examples.
In this post we will create the folders inside a list or document library using COM.
Create Folders In List :
using (ClientContext clientContext = new ClientContext(SiteURL)) { Web currentWeb = clientContext.Web; List list = currentWeb.Lists.GetByTitle("NewList"); list.EnableFolderCreation = true; FolderCollection folderColl = list.RootFolder.Folders; clientContext.Load(currentWeb); clientContext.Load(list); clientContext.Load(folderColl); clientContext.ExecuteQuery(); string folderName = "Folder1"; string newFolderUrl = list.Title + "/" + folderName; Folder existingFolder = null; string relativURL = currentWeb.ServerRelativeUrl; //Check if folder exists in the list if (list != null) { //Create the exact folder URL. String folderUrl = String.Format("{0}{1}/{2}", currentWeb.ServerRelativeUrl, list.Title, folderName); //If it is a pulishing site then //String folderUrl = String.Format("{0}{1}{2}/{3}", currentWeb.ServerRelativeUrl, "Lists/", list.Title, folderName); IEnumerable<Folder> existingFolders = clientContext.LoadQuery<Folder> (folderColl.Where(folder => folder.ServerRelativeUrl == folderUrl)); clientContext.ExecuteQuery(); existingFolder = existingFolders.FirstOrDefault(); } if (existingFolder == null) { //Get the collection of content types. ContentTypeCollection listContentTypes = list.ContentTypes; clientContext.Load(listContentTypes, types => types.Include (type => type.Id, type => type.Name, type => type.Parent)); //Get the Folder content type. var result = clientContext.LoadQuery(listContentTypes.Where (c => c.Name == "Folder")); clientContext.ExecuteQuery(); ContentType folderContentType = result.FirstOrDefault(); ListItemCreationInformation newItemInfo = new ListItemCreationInformation(); newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder; newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder; newItemInfo.LeafName = folderName; ListItem newListItem = list.AddItem(newItemInfo); newListItem["ContentTypeId"] = folderContentType.Id.ToString(); newListItem["Title"] = folderName; newListItem.Update(); clientContext.Load(list); clientContext.ExecuteQuery(); } }; |
In this following points are important to understand :
1) 1) Get the folders collection of the list using FolderCollection
<!2 Check if the folder you are about to create already exists or not .
Note : If it is a publishing site then use folder URL as
ServerRelative URL + “/Lists/” + ListTitle + folderName
//String folderUrl = String.Format("{0}{1}{2}/{3}", currentWeb.ServerRelativeUrl, "Lists/", list.Title, folderName);
<!3)Get the folder content type and finally create the item and assign the folder content type to the newly created item.
4) This will create folder in the list.
Check if List exists in the site :
When we try to access a list using GetListByTitle() , we are required to pass the list name as parameter, In this case if List does not exists then this throws exception, So it is always recommended to check if list exists in the site before doing any operation on the list.
Below code snippet will return List object if it exists otherwise it will return null.
private static List CheckIfListExists() { String listTitle = "NewList"; using (ClientContext clientContext = new ClientContext(SiteURL)) { List existingList; Web web = clientContext.Web; ListCollection lists = web.Lists; IEnumerable<List> existingLists = clientContext.LoadQuery( lists.Where( list => list.Title == listTitle) ); clientContext.ExecuteQuery(); existingList = existingLists.FirstOrDefault(); return existingList; } } |
Happy coding :)
No comments:
Post a Comment