2

I'm experiencing a weird error while trying to update the author and editor on some files in a site, but not all, and the behavior is strange and inconsistent.

The code:

SPListItem item;
int newID;

item[SPBuiltInFieldId.Author] = newID;
item[SPBuiltInFieldId.Editor] = newID;

item.UpdateOverwriteVersion();

I've also tried variations of using SPFieldUserValue objects, SPUser objects, item.SystemUpdate(false), etc. from different articles I've found online but no dice. The "User cannot be found" errors still occur. The users do exist and the metadata is updated on some folders using the same users.

This is working as expected on all folders in the document library without a hitch. It also works on about 2/3 of the files in the library. However the remaining number of files result in an exception stating "User cannot be found". The strange thing about this is that it appears to be throwing this error for specific users consistently, but those users also created/edited folders in the library which are updated successfully.

Examples:

Success:

A user is the author and editor of a document and also the folder that the document is in. The folder's author and editor properties and the file's author and editor properties are all updated successfully.

Failure:

A user is the author and editor of a document and also the folder that the document is in. The folder's author and editor properties are updated successfully, but the file's SPListItem throws a "User cannot be found" exception while trying to update the item.

What is wrong with this? Or how can I approach this in another way that will not result in these errors?

Edit:

Now here's an interesting turn of events.

After running the above code, for the successes, the user in the UI is updated, however the object model is giving me different results for the users from different methods.

SPFile file = web.GetFile(url);

file.Author <-- old author, from:
file.Properties["vti_author"] <-- old author
file.Item["Author"] <-- new author

file.Item["vti_author"] <-- System.ArgumentException

Which means none of the files are actually being updated all the way correctly... Haven't tested the folders yet.

Edit 2:

I found out the root of the error, though it doesn't quite make sense to me why this would be an issue while setting the value...

The problematic users are stored in SPFile.Properties["vti_author"] as being classic auth users missing the claims prefix, though via the UI if you click the link it takes you to the user information page of the proper claims user associated with that same user account.

This leads me to believe this is an environment specific issue either with a remnant of the prior 2007->2010 migration or the classic->claims move shortly thereafter. Fun.

  • If it's only happening to specific users, perhaps the user accounts are corrupted? Is there any other common factor like permissions or groups? Off the top of my head I would say inspect their AD accounts for anything fishy and then run a full sync. – thanby Jan 15 '14 at 17:00
  • What's ULS tell you? Anything more? – Mike Jan 15 '14 at 17:40
  • Where are you pulling the UserID from? – Mike Jan 15 '14 at 17:45
  • 1
    @Mike the last UPS sync was a last night, runs every night, no errors, but I don't think this is a User Profiles issue. There are no MySites configured on this server, and to be honest I'm not even sure why UPS is. The user information required for this should be contained to the site collection's UIL. – RJ Cuthbertson Jan 15 '14 at 21:39
  • 1
    Let's finish in chat! chat.stackexchange.com/rooms/12580/… – Mike Jan 20 '14 at 15:27
0

I ended up reading the file out of the library and writing it back again with a copy of all the metadata. This was the only way I could get all references to the old users to be removed.

For posterity, the code I used looked something like:

SPUser author = GetNewUser(new SPFieldUserValue(web, file.Item[SPBuiltInFieldId.Author].ToString()));
SPUser editor = GetNewUser(new SPFieldUserValue(web, file.Item[SPBuiltInFieldId.Editor].ToString()));
DateTime created = file.TimeCreated;
DateTime modified = file.TimeLastModified;

SPFileCollection files = file.ParentFolder.Files;
byte[] content;

using (Stream stream = file.OpenBinaryStream())
{
    content = new byte[stream.Length];
    stream.Read(content, 0, (int)stream.Length);
}

file.Delete();

SPFile newFile = files.Add(url, content, author, editor, created, modified);
foreach (KeyValuePair<string, object> pair in properties)
{
    newFile.Item[pair.Key] = pair.Value;
}
newFile.Item.SystemUpdate();
  • Did it resolve the issue? I see similar error while trying to access SPListItem.File.Author (User cannot be found). Scenario: when I move item from site1 to site2, but not getting error, if I move item from site2 to site1. Please help. – raju Oct 26 '17 at 15:20

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.