Timeline for Access is denied. inside my ItemAdded event receiver
Current License: CC BY-SA 3.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 7, 2016 at 0:35 | comment | added | John John | @GautamSheth so now i am confused what i need to do :( | |
Dec 6, 2016 at 11:22 | comment | added | Gautam Sheth |
Check the first two code snippets. There I am adding a site without opening SPWeb object. SPWeb newSite = elevatedSite.AllWebs.Add(curItemSiteName, curItemSiteName, "subsite was added automatically", Convert.ToUInt16(1033), webTemplate, false, false); You can try to add using this as well.
|
|
Dec 6, 2016 at 0:08 | comment | added | John John |
@GautamSheth since i did not implement such an event receiver before so i was trying to capture everything.. now i came across this link [link](msdn.microsoft.com/en-IN/library/ee724407(v=office.14).aspx) which talks about the best practices when creating event receivers and they mentioned that it is not recommended to instantiate an SPWeb, SPSite within an event receiver, which i am currently doing.. so can you adivce if there is a way to modify my event receiver to be following the best practices + still have the RunWithElevatedPrivileges in effect??
|
|
Dec 5, 2016 at 23:01 | comment | added | John John |
@GautamSheth but i have one final note before accepting your answer.. now i am not an expert in event receivers,, but when i want to access the current site inside the event receivers seems i should do it in this way using (SPSite site = new SPSite(properties.SiteId)) ,, but is there a way to get the current site from the properties instead of creating a new instance ??as this code looks like i am going to create/save a new site inside my event receiver!!
|
|
Dec 5, 2016 at 17:12 | comment | added | Gautam Sheth | yep looks good . | |
Dec 5, 2016 at 16:58 | comment | added | John John | @GautamSheth ok it will work on your updated code ,, but i have made this modification, where i replace the "SPWeb web = site.OpenWeb()" to be "SPWeb web = site.OpenWeb(properties.RelativeWebUrl)",, as i wanted the new subsites to be created under the /projects/ subsite and not directly inside the root site .. so is this fine ? | |
Dec 5, 2016 at 14:08 | comment | added | Gautam Sheth | @johnG - my bad. You were correct regarding the spweb object. Have updated the code. Should work with elevated permissions now. | |
Dec 5, 2016 at 14:04 | history | edited | Gautam Sheth | CC BY-SA 3.0 |
deleted 8 characters in body
|
Dec 5, 2016 at 12:29 | comment | added | Gautam Sheth | @johnG - i think it should work just fine. Havent tested it as my VM crashed. Can you test the code ? let me know if you face the same issue again, will recreate the it tomorrow as i would need to new environment. | |
Dec 5, 2016 at 12:20 | comment | added | John John | @GautamSheth Now if i use your updated code mainly opening the web in this way "SPWeb web = properties.OpenWeb()" will result in the elevated privileges being ignored, here is a similar issue link .. and i need to use this "SPWeb spCurrentSite = site.OpenWeb(currenweburl)" instead?? have you tested your updated code, as it will ignore the elevated privileges.. | |
Dec 5, 2016 at 6:21 | comment | added | Gautam Sheth | check updated full event receiver code now. Have also added @RobertLindgren 's suggestion to use properties | |
Dec 5, 2016 at 6:19 | history | edited | Gautam Sheth | CC BY-SA 3.0 |
added full event receiver code
|
Dec 5, 2016 at 0:03 | comment | added | John John | @Gautam can you please advice why i got this error this error :- "Microsoft.SharePoint.SPSite' does not contain a definition for 'Webs' and no extension method 'Webs' accepting a first argument of type 'Microsoft.SharePoint.SPSite' could be found (are you missing a using directive or an assembly reference?)" on "site.Webs.Add(".. so is this suppose to work | |
Dec 4, 2016 at 23:40 | comment | added | John John | @RobertLindgren thanks for the reply.. so how i should manage my case ? | |
Dec 4, 2016 at 16:24 | comment | added | Robert Lindgren |
@johng correct! You should be able to get the site and web from the properties instead
|
|
Dec 4, 2016 at 13:30 | comment | added | John John | @Gautam i tried your second approach but i got this error :- "'Microsoft.SharePoint.SPSite' does not contain a definition for 'Webs' and no extension method 'Webs' accepting a first argument of type 'Microsoft.SharePoint.SPSite' could be found (are you missing a using directive or an assembly reference?)" on "site.Webs.Add(".. so is this suppose to work ? | |
Dec 4, 2016 at 13:15 | comment | added | John John | @RobertLindgren so if i understand your point correctly , is that if an user create a new project item using console app or a timer job, then the SPContext inside the event receiver will be null,, which will cause my event receiver to raise an exception and thus no new sub-site is going to be created ?? if this is the case, then is there any workaround to force my event receiver to work well on console application and timer jobs ? | |
Dec 4, 2016 at 10:39 | comment | added | Robert Lindgren |
Big heads-up with using SPContext in event receivers. It will be null if the code is not called from a web process (like if called from a console application or timer job)
|
|
Dec 4, 2016 at 5:14 | history | answered | Gautam Sheth | CC BY-SA 3.0 |