2013년 1월 29일 화요일

How to add item to list subfolder using Client object model

How to add item to list subfolder using Client object model




 protected void AddItemInFolder(object sender, EventArgs e)
        {
            string siteURL = SPContext.Current.Web.Url;
            AddItemInFolders(siteURL, "Docs", "Folder1/Folder2");//subfolders
            AddItemInFolders(siteURL, "Docs", "Folder1");//root folder
        }

        private void AddItemInFolders(string siteUrl, string listName, string folderPath)
        {
            using (var clientContext = new ClientContext(siteUrl))
            {
                var list = clientContext.Web.Lists.GetByTitle("Docs");

                ListItemCreationInformation listItemCreationInformation = null;
                if (!string.IsNullOrEmpty(folderPath))
                {
                    listItemCreationInformation = new ListItemCreationInformation();
                    listItemCreationInformation.LeafName = folderName;
                    listItemCreationInformation.UnderlyingObjectType = FileSystemObjectType.Folder;
                    listItemCreationInformation.FolderUrl = string.Format("{0}/lists/{1}/{2}", siteUrl, listName, folderPath);
                }

                var listItem = list.AddItem(listItemCreationInformation);
                listItem["Title"] = "Titile1";
                listItem.Update();
                clientContext.ExecuteQuery();

            }
        }

댓글 없음: