16

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.

1 Answer 1

11

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?

4
  • 1
    BTW - it seems that the caller requires "Owner" or site collection admin permissions for this to work. Thanks to @jimmywim for pointing this out. Commented Oct 28, 2011 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
    Commented Nov 18, 2011 at 0:04
  • Confirmed that it works in our environment, too! Thanks again.
    – Sean Mahan
    Commented Nov 23, 2011 at 19:08
  • No worries. Glad I could be of help. Commented Nov 28, 2011 at 14:50

Your Answer

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

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