0

I'd like to upload a document to a SharePoint 2013 document library and set value for three of the columns.

I'm running following C# code from a unit test within Visual Studio:

using (var ctx = new ClientContext($"{spRoot}/{spPathToFolder}"))
            {
                ctx.Credentials = CredentialCache.DefaultNetworkCredentials;
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, targetFileUrl, ms, true);

                var uploadedFile = ctx.Web.GetFileByServerRelativeUrl(targetFileUrl);
                var listItem = uploadedFile.ListItemAllFields;

                listItem["Title"] = "title";
                listItem["UPRN"] = "uprn";
                listItem["KeystoneDocType"] = "keystoneDocType";
                listItem.Update();
                ctx.ExecuteQuery();
            }

The following path variables values are logged:

spRoot=[https://collaboration.xxx.com], spPathToFolder=[sites/HousingICTSolution/Technical]

targetFileUrl=[/sites/HousingICTSolution/Technical/AssetMgmtEfilesDemo/xxxLogo_190213115512.png]

On executing the line "ctx.ExecuteQuery()" there's a delay of about 2 seconds before control returns to the debugger. There are no errors but there's no sign of the document within the library. Anyone see where I went wrong?

  • try to do ctx.Update() after SaveBinaryDirect() – Zdeněk Vinduška Feb 14 at 10:53
  • it seems there is no Update method on the ClientContext object? – Rob Bowman Feb 14 at 11:36
  • i mean ctx.ExecuteQuery() :D – Zdeněk Vinduška Feb 14 at 11:45
  • I'm afraid that made no difference - no error but still no sign of the document appearing in the document library – Rob Bowman Feb 14 at 11:49
  • so there'S problem with saving the file to sharepoint, cause you need to save it, after this you will update properties. try find another solution for saving document to SPO – Zdeněk Vinduška Feb 14 at 13:35
0

I received an answer on SO, thanks to @Jerry_MSFT. I tweaked it a little, the following works ok:

public void UploadAndSetProps(string spRoot, string spPathToFolder, string spFolder, string spFilename, MemoryStream ms)
    {
        _log.WriteLine($"Entered UploadAndSetProps. spRoot=[{spRoot}], spPathToFolder=[{spPathToFolder}]");

        try
        {
            string targetFileUrl = $"/{spPathToFolder}/{spFolder}/{spFilename}";
            _log.WriteLine($"targetFileUrl=[{targetFileUrl}]");
            using (var ctx = new ClientContext($"{spRoot}/{spPathToFolder}"))
            {
                ctx.Credentials = CredentialCache.DefaultNetworkCredentials;
                using (ms)
                {
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, targetFileUrl, ms, true);
                    ctx.ExecuteQuery();
                }

                var uploadedFile = ctx.Web.GetFileByServerRelativeUrl(targetFileUrl);

                var listItem = uploadedFile.ListItemAllFields;
                listItem["Title"] = "title";
                listItem["UPRN"] = "uprn";
                listItem["KeystoneDocType"] = "keystoneDocType";
                listItem.Update();
                ctx.ExecuteQuery();

                listItem.File.CheckIn("Added by BizTalk", CheckinType.MajorCheckIn);
                ctx.ExecuteQuery();
            }
        }
        catch (Exception ex)
        {
            _log.WriteException(ex);
            throw ex;
        }

        _log.WriteLine("Leaving UploadAndSetProps");


    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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