使用MetaData字段上传文件

时间:2014-05-14 16:47:02

标签: sharepoint file-upload metadata sharepoint-2013

using (ClientContext clientContext = new ClientContext("URLSHAREPOINT"))
            {
                SecureString passWord = new SecureString();

                foreach (char c in "PASSWORD".ToCharArray()) passWord.AppendChar(c);

                clientContext.Credentials = new SharePointOnlineCredentials("USERNAME.onmicrosoft.com", passWord);
                Web web = clientContext.Web; 
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(@"FILELOCATION");
                newFile.Url = "file uploaded via client OM.txt";
                List docs = web.Lists.GetByTitle("LIBRARYNAME");
                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
                clientContext.ExecuteQuery();
}

我使用上面的代码将文件上传到我的Sharepoint 365帐户! 但我也需要添加一些元数据

有人可以帮助我使用我当前的代码添加元数据吗?

1 个答案:

答案 0 :(得分:1)

                clientContext.Load(docList);
                clientContext.Load(docList.Fields.GetByTitle("METADATANAME"));
                clientContext.Load(docList.Fields.GetByTitle("METADATANAME"));
                clientContext.ExecuteQuery();
                var Nome = docList.Fields.GetByTitle("METADATANAME").InternalName;
                var Posicao = docList.Fields.GetByTitle("METADATANAME").InternalName;
                uploadFile.ListItemAllFields[Nome] = "VALUE";
                uploadFile.ListItemAllFields[Posicao] = "VALUE";
                uploadFile.ListItemAllFields.Update();
                clientContext.Load(uploadFile);
                clientContext.ExecuteQuery();

这很有效!希望用这个来帮助一些人=)