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

Issue which i facing is follows.

I have attached sp2010 approval workflow to list on item created.

When i insert entry into list manually, and it display workflow status is In Progress, it means workflow runs perfectly.

now issue with insert entry programatically.

When i insert entry through code. there is not displaying workflow status in that list item. it means workflow not attached. and not working

My Code for inserting items is

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite spsite = new SPSite(siteurl))
            {
                using (SPWeb spweb = spsite.OpenWeb())
                {
                    SPList splist = spweb.Lists["My List"];
                    SPListItem oItem= splist.AddItem();;

                    oItem["Title"] = txtTitle.Text;
                    oItem["Author"] = objSPUser;
                    oItem["Editor"] = objSPUser;
                    oItem.Update();
                }
            }
        });

I need to use SPSecurity.RunWithElevatedPrivileges function because user has not rights for that list.

User can Insert records through dashboard. so i updating Created By and Modified by using System account.

How to resolve ?

or any other alternate way to do this instead SPSecurity.RunWithElevatedPrivileges

share|improve this question
    
Something must be happening to the event receiver that gets kicked off for on Item Added. I suspect a racing condition. Anyways, My suggestion would be to manually start the workflow when creating the list item manually. –  Fox Jan 11 '13 at 9:54
1  
afaik you cannot trigger a workflow if you use the System Account, try and impersonate another user –  Anders Aune Jan 11 '13 at 10:25
    
RunWithElevatedPriveleges should not be used to circumvent the SharePoint security model. If you need higher level access you should use user impersonation. –  RJ Cuthbertson Jan 11 '13 at 15:02
    
how to use user impersonation? any link ? if i use user impersonation, then can i update Created By and Modified By ? pls suggest me link for how to use user impersonation. thanks in advance buddy –  Hitesh Chandegara Jan 11 '13 at 17:11
add comment

1 Answer

Impersonate the user. Like this:

   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
        var userToken = SPContext.Current.Web.AllUsers["domain\\user"].UserToken;

        using (SPSite site = new SPSite(siteurl, userToken))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList splist = spweb.Lists["My List"];
                SPListItem oItem= splist.AddItem();;

                oItem["Title"] = txtTitle.Text;
                oItem["Author"] = objSPUser;
                oItem["Editor"] = objSPUser;
                oItem.Update();
            }
        }
    });
share|improve this answer
add comment

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.