根据内容类型创建新文档

时间:2014-01-23 15:04:59

标签: sharepoint sharepoint-2010 csom

如何使用CSOM以编程方式在文档库中创建新文档?新文档应来自文档模板。

            ClientContext clientContext = new ClientContext("http://localhost");
            Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle("CustomDocumentLibrary")

            var folder = list.RootFolder;
            clientContext.Load(folder);
            clientContext.ExecuteQuery();

            var cType = clientContext.LoadQuery(list.ContentTypes.Where(x => x.Name == "CustomContentType"));
            clientContext.ExecuteQuery();

            ContentType targetDocumentSetContentType = cType.FirstOrDefault();
            string contentTypeId = targetDocumentSetContentType.Id.ToString();

            // Change to load the document template from Content Type
            FileCreationInformation fileCreateInfo = new FileCreationInformation();
            byte[] fileContent = System.IO.File.ReadAllBytes(@"C:\files\template.docx");     
            fileCreateInfo.Content = fileContent;

            fileCreateInfo.Url = folder.ServerRelativeUrl + "/New_File.docx";

            Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(fileCreateInfo);
            clientContext.Load(list);
            clientContext.ExecuteQuery();


            ListItem newItem = uploadFile.ListItemAllFields;
            newItem["Title"] = "Title";
            newItem["ContentTypeId"] = contentTypeId;
            newItem.Update();
            clientContext.ExecuteQuery();

0 个答案:

没有答案