【问题标题】:Uploading the file to sharepoint and automatically check in the the file after upload using WebClient将文件上传到共享点,并在使用 WebClient 上传后自动签入文件
【发布时间】:2023-04-08 05:20:01
【问题描述】:

使用 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();
            }

【问题讨论】:

标签: c# sharepoint


【解决方案1】:

我认为您有一些必填字段,您必须在签入文件之前对其进行设置。

签到 使用 Microsoft.Sharepoint.Client.ListItem.File.CheckIn(...) 函数

【讨论】:

    猜你喜欢
    • 2012-11-06
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2012-04-15
    相关资源
    最近更新 更多