Setting author field in SPListItem won't persist - sharepoint

I am trying to copy an SPListItem (with file) from one site collection to another. I do this by creating the file like this:
var archiveFile = newsArchive.Lists[listName].RootFolder.Files.Add(originalItem.File.Name, originalItem.File.OpenBinary());
var archiveItem = archiveFile.Item;
through a utility method I wrote i then set all field values of the new item to correspond with the original item like so
Utilities.PopulateListItemMetadata(....)
The thing is, this does not persist the Author field.
I tried setting the Author field explicitely in every way imaginable, for instance like so:
string userName = originalItem.GetUser("Created by").LoginName;
SPUser user = newsArchive.SiteUsers[userName];
archiveItem["Author"] = user.ID + ";#" + user.LoginName;
archiveItem.Update();
And like so
string userName = originalItem.GetUser("Created by").LoginName;
SPUser user = newsArchive.SiteUsers[userName];
archiveItem["Author"] = user;
archiveItem.Update();
But as soon as the SPListItem.Update() method is called, the archiveItem["Author"] field has reverted to sharepoint\system. I'm a bit at a loss here, this should work..
P.S. the SPListItem.GetUser method is an extension method
P.P.S. Code is being run from a timer job...
Edit: Did some more digging by adding a new field to the content type and then setting that field to reflect the Author of the original item, but that is not set either. However, the web.EnsureUser(username) does return the correct user. Is this weird or what!?!

Found the answer, using
SPFieldUserValue val = new SPFieldUserValue(newsArchive, user.ID, user.Name);
archiveItem["Author"] = val;
archiveItem.SystemUpdate(false);
did the trick!

I've experienced the same problem. Have a look at this question.
The way that worked for me is to wrap the same code you have in your final example in elevated privileges.
Edit
Why don't you try replacing:
SPUser user = newsArchive.SiteUsers[userName];
with:
SPUser user = newsArchive.EnsureUser(userName);
Then you will know the user is in the web and also get a reference to them. The SiteUsers collection gives you the users in the site collection - they have not necessarily been added to the web. If SharePoint doesn't find the user it will probably use system account.

Related

Is it possibile to update an entity saved into my database with Graph?

i am using the Graph library in order to save my data correctly.
I was wondering, if there's a way to update an existing Entity, without duplicate the entity 2 times: user will be allowed to update just two values of the entity.
Also, i would like to know if there's a proper save method
I give you an example
let person = Entity(type: "Person")
person["name"] = //not editable
person["work"] = //editable
person["age"] = //editable
graph.sync() //or something like graph.update
Writing this, i am just creating a new entity, which is not what i want. Maybe, i have to search for the entity, delete that every time, and insert the new one?Hope not.
Thank you for any help you could give!
yes, you can update Entities.
You need to search for the one you want to update first. For example:
let graph = Graph()
let search = Search<Entity>(graph: graph).for(types: "Person")
for entity in search.sync() {
// do something
}
Once you have the entity you want to update, you can set any property, group, or tag as per usual.
entity["name"] = "Daniel"
From there you need to call graph.sync() or graph.async() so that all is saved.
Thats it :)

Can't set Orchard field values unless item already created

I seem to be having a problem with assigning values to fields of a content item with a custom content part and the values not persisting.
I have to create the content item (OrchardServices.ContentManager.Create) first before calling the following code which modifies a field value:
var fields = contentItem.As<MyPart>().Fields;
var imageField = fields.FirstOrDefault(o => o.Name.Equals("Image"));
if (imageField != null)
{
((MediaLibraryPickerField)imageField).Ids = new int[] { imageId };
}
The above code works perfectly when against an item that already exists, but the imageId value is lost if this is done before creating it.
Please note, this is not exclusive to MediaLibraryPickerFields.
I noticed that other people have reported this aswell:
https://orchard.codeplex.com/workitem/18412
Is it simply the case that an item must be created prior to amending it's value field?
This would be a shame, as I'm assigning this fields as part of a large import process and would inhibit performance to create it and then modify the item only to update it again.
As the comments on this issue explain, you do need to call Create. I'm not sure I understand why you think that is an issue however.

Get SPListItem in another format

I want to get different Rows from an SPListItem. I'll show you my problem with an example.
This code
Console.WriteLine(SPItemName["Created By"]);
or
Console.WriteLine(SPItemName["Created By"].ToString);
returns "8;UserName" (8 is the User ID).
If I look up the row in SharePoint Designer, i can choose even a format for this data field.
So i could get the html code of this field.
How to set the format (like html code or text) of a datafield in c#?
thanks
Use Either SPFieldLookupValue
If you need just the username, use SPFieldLookupValue to seperate id from value:
var userValue = new SPFieldLookupValue(SPItemName["Created By"] as string)
Then you can:
userValue.LookupValue to return UserName
userValue.LookupId to return Id
Or SPFieldUserValue
Or better yet, you can create SPFieldUserValue object to access any other user properties like email, login name, etc..
SPFieldUserValue objUserFieldValue = new SPFieldUserValue(web, SPItemName["Created By"].ToString());
Afterwards you can use:
objUserFieldValue.User.LoginName;
objUserFieldValue.User.Name;
objUserFieldValue.User.ID;
objUserFieldValue.User.Groups;
objUserFieldValue.User.Roles;
objUserFieldValue.User.Email;
objUserFieldValue.User.Sid;
objUserFieldValue.User.UserToken;
http://www.sharepointkings.com/2009/04/spfielduservalue-and.html
Note: to create SPFieldUserValue you must pass reference to web, that's because SharePoint has to get additional user information from user information list to construct SPFieldUserValue object.

Getting RoleCollection as a string

We can get the roles of an SPUser by SPUser.Roles. But it will return SPRoleCollection. If we want to list all the roles we need to loop that.
For example an User has "Full Control","Read","Design" we need to loop the SPRoleCollection object.
How can i get all the roles as a string with ',' separator?
As a rough guess, try:
var user = SPUser // However you get the user.
var roles = Sring.Join(",", (from r in user.Roles select r.Name).ToArray()));
Though if you're using SharePoint 2010, the Name property is obsolete apparently.

SharePoint : Guessing the attachment path before updating a list item

I have some code that inserts a list item into a list...
I then have this code
SPFolder folder = web.Folders["Lists"].SubFolders[list.RootFolder.Name].SubFolders["Attachments"].SubFolders[item.ID.ToString()];
foreach (SPFile file in folder.Files)
{
string attachmentName = this.downloadedMessageID + ".xml";
if (file.Name == attachmentName)
{
SPFieldUrlValue value = new SPFieldUrlValue();
value.Description = this.downloadedMessageID + ".xml";
value.Url = this.SiteAddress + file.Url;
item["ZFO"] = value;
}
}
this is fine except for one problem... before this code actually works... I need to call the item.update() method to save the item to SharePoint...
But as you can see there is more work to do ... after item.update is called...
So this means... I have
work
item.update();
more work
item.update();
The problem I am having is I really want just
work
item.update();
So that in any event of failure the whole thing will fail at once or pass at once.... (almost like a SQL transaction).
So whats preventing me from doing this is - I need to set a hyperlink to one of the fields in the list item, this will be to an attachment in the list attachments collection.
Is there any way I can predict this address without having saved the list item to MOSS?
An attachment path depends on the item ID, and I don't believe your item will have an ID until you save it. Have you considered storing the attachments in a document library instead, linked by the field you're trying to set?
Transactional operation isn't exactly SharePoint's strong suit.

Resources