Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to insert new SP List items -- blog comments, to be exact -- on behalf of other users.

Ideally, I'd just pass a SharepointID for the user, but that doesn't seem to be possible. I think that Impersonation might be the most likely way to do this, but while I've found documentation on using user tokens for this within sharepoint applications, I haven't found a way to do this through the client object model.

Background: I'm writing a C# Windows Service that fetches email, parses it, and then inserts the contents as a comment on a given blog post. I'll only have the user's email address at first, but I can use that to grab their AD username, SharepointID, and presumably any other info I'd need to create the comment on their behalf -- except, of course, for their password.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

EDIT I have now tried this from within a console application local to my SharePoint VM. I can confirm it works in that environment (both "Created By" and "Modified By" were updated). Let us know if you run into issues using it from within your Windows Service.

If I understand your requirement correctly you could most likely update the list item after the fact to ensure that the comment metadata shows the correct username.

The code sample below comes from this forum post. I haven't tested it and it's not necessarily the best or only way of doing it:

ClientContext ctx = new ClientContext("http://siteurl");
List list = ctx.Web.Lists.GetByTitle("ListName");
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); 
ctx.ExecuteQuery();

User user = ctx.Web.EnsureUser("ben");

foreach (var item in items)
{
       // Created by and edited by
       item["Author"] = user;
       item["Editor"] = user;

       item.Update(); 
}
ctx.ExecuteQuery(); 

Does that fit the bill?

share|improve this answer
    
BTW - it seems that the caller requires "Owner" or site collection admin permissions for this to work. Thanks to @jimmywim for pointing this out. –  Benjamin J Athawes Oct 28 '11 at 8:59
    
Hey, thanks Benjamin! Only just seeing this now -- I assumed I'd get an email about any responses this received. For the moment, we'd just skipped this issue, but I'll trying doing things this way and report back. –  Sean Mahan Nov 18 '11 at 0:04
    
Confirmed that it works in our environment, too! Thanks again. –  Sean Mahan Nov 23 '11 at 19:08
    
No worries. Glad I could be of help. –  Benjamin J Athawes Nov 28 '11 at 14:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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