将文件上载到sharepoint并使用WebClient在上载后自动签入文件

时间:2013-06-28 13:05:51

标签: c# sharepoint

使用webclient PUT方法将文件上传到Sharepoint,上传但是以我的名字检出。我们如何自动检入文件?

请建议我如何解决问题 下面是将文件上传到sharepoint的代码。

public void UploadToSharepoint()
        {

                GC.Collect();
                GC.WaitForPendingFinalizers();

                var site = string.Empty;
                var serviceAccountUsername = string.Empty;
                var serviceAccountPassword = string.Empty;
                var serviceAccountDomain = string.Empty;

                var documentToUpload = ConfigurationManager.AppSettings[AppConstants.DocumentToUpload].ToString();
                var documentLibrary = ConfigurationManager.AppSettings[AppConstants.documentLibrary].ToString();

                int year = DateTime.Now.Year;
                int month = DateTime.Now.Month;

                var targetDocumentName = year + "_" + month + "_ABCD.xlsx";

                if (!System.IO.File.Exists(documentToUpload))
                    throw new FileNotFoundException("File Not Found", documentToUpload);

                using (var client = new WebClient())
                {
                    client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

                    site = ConfigurationManager.AppSettings[AppConstants.SiteUrl].ToString();

                    serviceAccountUsername = ConfigurationManager.AppSettings[AppConstants.ServiceAccountUsername].ToString();
                    serviceAccountPassword = ConfigurationManager.AppSettings[AppConstants.ServiceAccountPassword].ToString();
                    serviceAccountDomain = ConfigurationManager.AppSettings[AppConstants.ServiceAccountDomain].ToString();

                    if (!client.UseDefaultCredentials)
                    {
                        var networkCredential = new NetworkCredential
                        {
                            Domain = serviceAccountDomain,
                            UserName = serviceAccountUsername,
                            Password = serviceAccountPassword
                        };

                        client.Credentials = networkCredential;
                    }

                    var bytes = System.IO.File.ReadAllBytes(documentToUpload);
                    var uri = new Uri(String.Format("{0}/{1}/{2}", site, documentLibrary, targetDocumentName));

                    client.UploadData(uri, "PUT", bytes);
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

1 个答案:

答案 0 :(得分:0)

我认为你有一些必填字段,你必须在检查文件之前设置它们。

办理登机手续 使用Microsoft.Sharepoint.Client.ListItem.File.CheckIn(...)函数