3

I am trying to update the Microsoft.SharePoint.Client.File object Author and Editor property after successful update. below code I have written for that.

foreach (Attachment att in attachments)
{
    Microsoft.SharePoint.Client.File oFile = ctx.Web.GetFileByServerRelativeUrl(att.ServerRelativeUrl);
    var result = Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(ctx, ctx.Web, "[email protected]", Microsoft.SharePoint.Client.Utilities.PrincipalType.User, Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
    ctx.ExecuteQuery();
    if (result != null)
    {
        var user = ctx.Web.EnsureUser(result.Value.LoginName);
        ctx.Load(user);
        ctx.ExecuteQuery();
        //to check what is the current value for this
        ctx.Load(oFile.Author);
        ctx.ExecuteQuery();
        //to check what is the current value for this
        ctx.Load(oFile.ModifiedBy);
        ctx.ExecuteQuery();
        //New value for Update
        oFile.ListItemAllFields.FieldValues["Author"] = user;
        oFile.ListItemAllFields.Update();//getting error on this line
        ctx.ExecuteQuery();
        //New value for Update
        oFile.ListItemAllFields.FieldValues["Editor"] = user;
        oFile.ListItemAllFields.Update();
        ctx.ExecuteQuery();
        ctx.Load(oFile.Author);
        ctx.ExecuteQuery();
        ctx.Load(oFile.ModifiedBy);
        ctx.ExecuteQuery();
    }
}

Below is the error message am getting.

Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "ListItemAllFields".

I need help where am going wrong? or any light on how do I can update the Microsoft.SharePoint.Client.File metadata property using CSOM.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.