Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I successfully got metadata from Sharepoint files to display and used the built-in MoveCopyOptions to move files from one SharePoint folder to another.

I now want to be able to add files programmatically to SharePoint and use the information inputted from a form by the user to populate the metadata.

The file is copied from the user's local drive to the share point folder however I am not able to update the metadata. Could someone let me know if they can spot what is wrong?

Thank you in advance.

What I have tried:

public bool CopyLocalFileToSP(string localPath, string fileName, string caseNumber, string userName, string comment)
{
    try
    {
        using (var clientContext = new ClientContext(_helper.WebRoot()))
        {
            var web = clientContext.Web;

            clientContext.Load(web, website => website.ServerRelativeUrl);
            clientContext.ExecuteQuery();

            var serverRelativeURL = web.ServerRelativeUrl + "/" + "Documents/" + caseNumber.Substring(0, caseNumber.IndexOf("-")) + "/" + caseNumber + "/" + fileName;

            using (FileStream fs = new FileStream(Path.Combine(localPath, fileName), FileMode.OpenOrCreate))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeURL, fs, true);
            }

            Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(serverRelativeURL);
            clientContext.Load(newFile);
            clientContext.ExecuteQuery();


            newFile.CheckOut();

            ListItem item = newFile.ListItemAllFields;


            item["FileLeafRef"] = fileName;
            item["Comment1"] = comment;
            item["DocumentDate"] = DateTime.Now;
            item["Created"] = DateTime.Now;
            item["Modified_x0020_By"] = userName;
            item["DocumentType"] = "Member";
            item.Update();

            newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
        }

        return true;
    }
    catch <pre><pre lang="C#">

{
return false;
}
}
Posted
Updated 19-Jul-23 23:59pm

1 solution

try
            {
                using (var clientContext = new ClientContext(_helper.WebRoot()))
                {
                    var web = clientContext.Web;

                    clientContext.Load(web, website => website.ServerRelativeUrl);
                    clientContext.ExecuteQuery();

                    var serverRelativeURL = web.ServerRelativeUrl + "/" + "Documents/" + caseNumber.Substring(0, caseNumber.IndexOf("-")) + "/" + caseNumber + "/" + fileName;

                    using (FileStream fs = new FileStream(Path.Combine(localPath, fileName), FileMode.OpenOrCreate))
                    {
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeURL, fs, true);
                    }

                    Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(serverRelativeURL);
                    clientContext.Load(newFile);
                    clientContext.ExecuteQuery();

                    newFile.CheckOut();


                    newFile.ListItemAllFields["FileLeafRef"] = fileName;
                    newFile.ListItemAllFields["Comment1"] = comment;
                    newFile.ListItemAllFields["DocumentType"] = "Favourites";
                    newFile.ListItemAllFields["Modified_x0020_By"] = userName;
                    newFile.ListItemAllFields.Update();
                    clientContext.ExecuteQuery();

 
                    newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
 
Share this answer
 
Comments
Richard Deeming 20-Jul-23 8:50am    
An unexplained code-dump is not a good solution. You need to explain what you have changed, and how it has fixed your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900