Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemUpdateAsync causes "Property ListId was not loaded" #570

Closed
jansenbe opened this issue Oct 11, 2021 Discussed in #569 · 9 comments
Closed

SystemUpdateAsync causes "Property ListId was not loaded" #570

jansenbe opened this issue Oct 11, 2021 Discussed in #569 · 9 comments
Assignees
Labels
area: model 📐 Related to the core SDK models question Further information is requested

Comments

@jansenbe
Copy link
Contributor

Discussed in #569

Originally posted by berntrune October 11, 2021
I am trying to update Modifed field using PNP SystemUpdateAsync.
When using UpdateAsync, all normal item fields are stored successfully (except Modified of course).
When changing to SystemUpdateAsync I get "Property ListId was not yet loaded". (see stack trace below)

using (var ctx = await _pnpContextFactory.CreateAsync(new Uri("http://site")))
{
var doc = await ctx.Web.Lists.GetByTitleAsync("Documents");
var file = await doc.RootFolder.Files.AddAsync("file.docx", fileStream, true);
file.ListItemAllFields["Title"] = "new title";
file.ListItemAllFields["Modified"] = DateTime.Now;
//await file.ListItemAllFields.UpdateAsync();
await file.ListItemAllFields.SystemUpdateAsync();
}

at PnP.Core.Model.TransientObject.GetValue[T](String propertyName)
at PnP.Core.Model.SharePoint.File.get_ListId()
at PnP.Core.Model.SharePoint.ListItem.d__80.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at PnP.Core.Model.SharePoint.ListItem.d__83.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at LargeFileTransfer.TransferFile.d__3.MoveNext() in C:\DevopsGit\Bikube.Azure.Services.BergenFunction\TransferFile.cs:line 109

Normally, in csom, I've been able to swap these methods without any issues.
Any clues?

@jansenbe jansenbe added bug Something isn't working area: model 📐 Related to the core SDK models labels Oct 11, 2021
@jansenbe jansenbe self-assigned this Oct 11, 2021
@jansenbe
Copy link
Contributor Author

@berntrune: You cannot simply update the Modified property, you always need to pass along the 4 properties (Author, Editor, Modified, Created).

See #522 (comment) for more context and a working sample.

Can you try that and see if it works.

The fact that you did not get an error calling Update() means that updating the modified field most likely also did not happen.

@berntrune
Copy link

@jansenbe Thanks for the sample. I added the other system properties with no luck. In my scenario I'm creating a new file and saving system properties, not opening an existing item and saving system properties. Can that be the issue? Do you have a test for this scenario?

@jansenbe
Copy link
Contributor Author

jansenbe commented Oct 12, 2021

Hi @berntrune,

Can you try below sample, this works for me in my lab for changing the created/modified/title properties of a document

var doc = await context.Web.Lists.GetByTitleAsync("Documents", p => p.Fields);
// Use below approach to also update the Author/Editor fields
//var doc = await context.Web.Lists.GetByTitleAsync("Documents", p => p.Fields);
var file = await doc.RootFolder.Files.AddAsync("ClassifyAndExtractFile.docx", System.IO.File.OpenRead($".{System.IO.Path.DirectorySeparatorChar}TestAssets{System.IO.Path.DirectorySeparatorChar}test.docx"), true);

// Load the file again to get the ListItemFields populated
file = await context.Web.GetFileByServerRelativeUrlAsync(file.ServerRelativeUrl, p => p.ListItemAllFields);

// Load the current user (e.g. when setting editor and author)
// var currentUser = await context.Web.GetCurrentUserAsync();
var newDate = new DateTime(2020, 10, 20);

// Load the Author and Editor fields
//var author = doc.Fields.AsRequested().FirstOrDefault(p => p.InternalName == "Author");
//var editor = doc.Fields.AsRequested().FirstOrDefault(p => p.InternalName == "Editor");

file.ListItemAllFields["Title"] = "new title";
file.ListItemAllFields["Created"] = newDate;
file.ListItemAllFields["Modified"] = newDate;
// Optionally also set the author and editor
//file.ListItemAllFields["Author"] = author.NewFieldUserValue(currentUser);
//file.ListItemAllFields["Editor"] = editor.NewFieldUserValue(currentUser);

await file.ListItemAllFields.UpdateOverwriteVersionAsync();

@jansenbe jansenbe added question Further information is requested and removed bug Something isn't working labels Oct 12, 2021
@jansenbe
Copy link
Contributor Author

@berntrune : doing some more digging I found a related bug. After adding a file one needs to load the ListItemAllFields property and that failed using await file.LoadAsync(p => p.ListItemAllFields); as approach (while the approach shown above worked). I've fixed this and with tomorrow's nightly build both approaches should work. I'll add a note to the docs to clarify that the need for the ListItemAllFields load after adding a file.

jansenbe added a commit that referenced this issue Oct 12, 2021
@jansenbe
Copy link
Contributor Author

@berntrune : are you good with closing this one?

@berntrune
Copy link

@jansenbe : Not tested complete yet, can we keep it open a couple of days more?

@jansenbe
Copy link
Contributor Author

@berntrune : any update?

@jansenbe
Copy link
Contributor Author

@berntrune : closing this one, feel free to re-open if the issue is not solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: model 📐 Related to the core SDK models question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants