|
I discovered that you cannot simply pass an impersonated SPWeb object into the SPServiceContext overload constructor and a little bit of 'black-magic' aka reflection is required thanks to Steven Curran SPService Context is used to do most things with the Social aspects of SP2010 such as Note Board comments (SocialCommentManager).
using (SPSite site = new SPSite(siteAddress))
{
using (SPWeb web = site.OpenWeb())
{
SPUser user = web.EnsureUser(domain +"\\"+ username);
web.AllowUnsafeUpdates = true;
HttpRequest request = new HttpRequest("", siteAddress, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));
HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
WindowsIdentity wi = WindowsIdentity.GetCurrent();
typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(wi, user.LoginName);
HttpContext.Current.User = new GenericPrincipal(wi, new string[0]);
WindowsIdentity wi2 = WindowsIdentity.GetCurrent();
SocialCommentManager socialCommentManager = new SocialCommentManager(SPServiceContext.GetContext(HttpContext.Current));
socialCommentManager.AddComment(new Uri("http://basesmc2008/SitePages/How%20To%20Use%20This%20Library.aspx"), "mycomment");
web.AllowUnsafeUpdates = false;
}
}
External References
|
How to do impersonation with SPServiceContext

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare