Question : How to Set Permission in Event Receiver in sharepoint ?

Hii All,
  I am using wss 3.0. And i have two lists.And i made one small event receiver and i am updating ListB from ListA by Event receiver. Now Some user has Contribute permission for both lists so they can easily update ListB from ListA. But Some user Has read only permission for LIStB And they have Write Permission for ListA. So they are not able to update ListB from ListA.

So I need to run Event Receiver as a Admin or Another user so they can update..

Answer : How to Set Permission in Event Receiver in sharepoint ?

Use SPSecurity.RunWithElevatedPrivileges to create item in list B. See the attached code snippet as an example.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
        private int CreateListB(SPListItem listA)
        {
            int intRet = 0;
 
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(listA.Web.Site.ID))
                {
                    SPWeb web = site.OpenWeb(listA.Web.ID);
 
                    SPListItem listB = web.Lists["List B"].Items.Add();
 
                    listB["List B Field 1"] = listA["List A Field 1"];
                    listB["List B Field 2"] = listA["List A Field 2"];
                    listB["List B Field 3"] = listA["List A Field 3"];
                    listB["List B Field 4"] = listA["List A Field 4"];
 
                    listB.Update();
 
                    intRet = listB.ID;
                }
            }
            );
 
            return (intRet);
        }
Open in New Window Select All
Random Solutions  
 
programming4us programming4us