1

Event receiver always running on privilages of acount who logged in. How to change in Event Receiver Account with who is firing the account. How could I set a account for firing event receiver ? By the code. Please help.

UPDATE

I have got code, and it give me error:

unauthorizedaccessexception was unhandled by user code But i Tried to put a user with previlages:

        public override void ItemUpdated(SPItemEventProperties properties)
    {
            this.EventFiringEnabled = false;
            base.ItemUpdated(properties);
            if (properties.List.Title == "Jawna")
            {
                using (SPSite site = new SPSite(properties.WebUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            SPList lstOtherList = properties.Web.Lists["Niejawna"];
                            string Tytuł = properties.ListItem["Title"].ToString();
                            string UnikalnaNazwa = properties.ListItem["UnikalnaNazwa"].ToString();
                            string fieldB = properties.ListItem["B"].ToString();
                            string fieldStatus = properties.ListItem["Status"].ToString();

                            SPQuery query = new SPQuery();
                            query.Query = "<Where><Eq><FieldRef Name='UnikalnaNazwa'/><Value Type='Text'>" + UnikalnaNazwa + "</Value></Eq></Where>";

                            SPListItemCollection items = lstOtherList.GetItems(query);
                            SPListItem item = items[0];

                            item["Title"] = properties.AfterProperties["Title"];
                            item["B"] = properties.AfterProperties["B"];
                            item["Status"] = properties.AfterProperties["Status"];
                            item.Update();
                        });
                    }
                }
            }
            if (properties.List.Title == "Niejawna")
            {
                using (SPSite site = new SPSite(properties.WebUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            SPList lstOtherList = properties.Web.Lists["Jawna"];

                            string Tytuł = properties.ListItem["Title"].ToString();
                            string UnikalnaNazwa = properties.ListItem["UnikalnaNazwa"].ToString();
                            string fieldB = properties.ListItem["B"].ToString();
                            string fieldStatus = properties.ListItem["Status"].ToString();

                            SPQuery query = new SPQuery();
                            query.Query = "<Where><Eq><FieldRef Name='UnikalnaNazwa'/><Value Type='Text'>" + UnikalnaNazwa + "</Value></Eq></Where>";

                            SPListItemCollection items = lstOtherList.GetItems(query);
                            SPListItem item = items[0];

                            item["Title"] = properties.AfterProperties["Title"];
                            item["B"] = properties.AfterProperties["B"];
                            item["Status"] = properties.AfterProperties["Status"];
                            item.Update();
                        });
                    }
                }
            }
            this.EventFiringEnabled = true;
    }

UPDATE 2

I tried it to, but still geting this error:

        public override void ItemUpdated(SPItemEventProperties properties)
    {
            this.EventFiringEnabled = false;
            base.ItemUpdated(properties);
            if (properties.List.Title == "Jawna")
            {
                                            SPSecurity.RunWithElevatedPrivileges(delegate()
                                            {
                                    using (SPSite site = new SPSite(properties.WebUrl))
                                    {
                                        using (SPWeb web = site.OpenWeb())
                                        {
                                                SPList lstOtherList = properties.Web.Lists["Niejawna"];
                                                string Tytuł = properties.ListItem["Title"].ToString();
                                                string UnikalnaNazwa = properties.ListItem["UnikalnaNazwa"].ToString();
                                                string fieldB = properties.ListItem["B"].ToString();
                                                string fieldStatus = properties.ListItem["Status"].ToString();

                                                SPQuery query = new SPQuery();
                                                query.Query = "<Where><Eq><FieldRef Name='UnikalnaNazwa'/><Value Type='Text'>" + UnikalnaNazwa + "</Value></Eq></Where>";

                                                SPListItemCollection items = lstOtherList.GetItems(query);
                                                SPListItem item = items[0];

                                                item["Title"] = properties.AfterProperties["Title"];
                                                item["B"] = properties.AfterProperties["B"];
                                                item["Status"] = properties.AfterProperties["Status"];
                                                item.Update();

                                        }
                                    }
                        });
            }
            if (properties.List.Title == "Niejawna")
            {
                                            SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite site = new SPSite(properties.WebUrl))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {

                                        SPList lstOtherList = properties.Web.Lists["Jawna"];

                                        string Tytuł = properties.ListItem["Title"].ToString();
                                        string UnikalnaNazwa = properties.ListItem["UnikalnaNazwa"].ToString();
                                        string fieldB = properties.ListItem["B"].ToString();
                                        string fieldStatus = properties.ListItem["Status"].ToString();

                                        SPQuery query = new SPQuery();
                                        query.Query = "<Where><Eq><FieldRef Name='UnikalnaNazwa'/><Value Type='Text'>" + UnikalnaNazwa + "</Value></Eq></Where>";

                                        SPListItemCollection items = lstOtherList.GetItems(query);
                                        SPListItem item = items[0];

                                        item["Title"] = properties.AfterProperties["Title"];
                                        item["B"] = properties.AfterProperties["B"];
                                        item["Status"] = properties.AfterProperties["Status"];
                                        item.Update();

                                }
                            }
                        });
            }
            this.EventFiringEnabled = true;
    }
  • 2
    ECM4D's answer is correct. Note that answer is creating new spweb and spsite objects inside the runWithElevatedPrivileges block. In your code above, you are using the existing spweb object that was outside of the block. – Mike2500 May 27 '15 at 11:48
  • I tried it too but still getting this error. I updated my Question. Look on update 2 – Grzegorz Z May 27 '15 at 12:08
  • 2
    You created a new spweb object, but then you didn't use it. You're using properties.Web, which was passed in via the parameter and was not created in the elevated block. – Mike2500 May 27 '15 at 12:23
4

You can impersonate in the event receiver. Usually, running code under System User is required and it is done using throuth RunWithElevatedPrivileges method, like:

Guid siteGuid = properties.SiteId;
Guid webGuid = properties.Web.ID;
Guid listGuid = properties.ListId;
int listItemId = properties.ListItemId;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite elevSite = new SPSite(siteGuid))
    using (SPWeb elevWeb = elevSite.OpenWeb(webGuid))
    {

        SPList elevList= elevWeb.Lists[listGuid];
        SPListItem elevListItem= elevList.GetItemById(listItemId);

        // Your code
    }
}

You can look also: Event Reciever run with elevated previledges

If you need really impersonate another specific user, you should use impersonation functionality. Here is some useful links:

  1. https://anujpant.wordpress.com/2012/11/05/sharepoint-user-impersonation-with-spusertoken/
  2. http://www.sharepointdeveloperhq.com/2009/04/how-to-programmatically-impersonate-users-in-sharepoint/
  • So only I should do it by: SPSecurity.RunWithElevatedPrivileges(delegate() { – Grzegorz Z May 27 '15 at 8:51
  • 2
    Yeh, it is the most common way to impersonate to user with higher permissions. On the other hand, I added another link how to impersonate with another specific user. – ECM4D May 27 '15 at 9:13
  • 2
    Please, ensure that you reinitialize SPSite and SPWeb object inside delegate, as showed in my example. Maybe you could share some code? – ECM4D May 27 '15 at 11:25
  • 2
    You have to use your elevated SPSite and SPWeb objects inside you delegate. Example, do not use properties.Web.Lists["Jawna"]; , use web.Lists["Jawna"]; – ECM4D May 27 '15 at 12:13
  • 2
    When you are inside event receiver your SPWeb object is in the same context of current user. When you impersonate a user (using RunWithElevated delegate), you have take new SPWeb object that is with higher permissions and use it – ECM4D May 27 '15 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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