I have a scenario where users with no permissions on a list be able to add items/modify items on the list. I am trying to use SpUserToken to achieve this but the list item is either created using system account or the user is denied access, Whereas I need the actual user in the created column. Thanks in Advance.
Access Denied While Item Update:-
protected void ImpersonationDemo(){
// This could be inside SPSecurity.RunWithElevatedPrivileges
// if you need to raise permissions initially
using (SPSite site = new SPSite(config.strUrl))
{
using (SPWeb web = site.OpenWeb())
{
#region Get LoggedIn User
SPUser spUser = web.CurrentUser;
#endregion
// Add a new list item by User 1
Impersonate(strUserName, web);
}
}
}
protected void Impersonate(string LoginID, SPWeb Web)
{
// Get User Token for the user we are trying to impersonate
SPUserToken token = Web.AllUsers["i:"+ LoginID].UserToken;
// IMPORTANT - Open a new site using this token
using (SPSite site = new SPSite(config.strUrl, token))
{
using (SPWeb w = site.OpenWeb())
{
SPList Announcement = w.Lists["Help Form List"];
SPListItem Item = Announcement.AddItem();
Item["Title"] = "Test";
Item.Update();
}
}
}
Item Created as System Account :-
private void usrtokentest(){
SPUserToken systemAccountUserToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite elevatedSite = new SPSite(config.strUrl, systemAccountUserToken))
{
using (SPWeb w = elevatedSite.OpenWeb())
{
w.AllowUnsafeUpdates = true;
SPList Announcement = w.Lists["Announcement List"];
SPListItem Item = Announcement.AddItem();
Item["Title"] = "Test";
Item.Update();
w.AllowUnsafeUpdates = false;
}
}
}