Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Uploading the file to Sharepoint by using webclient PUT method its getting uploaded but checked out in my name.How we can checked in the file automatically??

Please suggest me how to fix the issue Below is the code wich upload the file to 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();
            }
share|improve this question
add comment

1 Answer

I think you have some required fields, to them you have to set before check in the file.

to check in use Microsoft.Sharepoint.Client.ListItem.File.CheckIn(...) Function

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.