Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Is there a way to bypass the users' permission checking inside my event receiver to fix "Access Defined" error

$
0
0

I am working on SharePoint Server 2013 Enterprise, and I have implemented the following event receiver which is scoped at the Web level, and will fire when a list item is added:-

public override void ItemAdded(SPItemEventProperties properties)
        {

            base.ItemAdded(properties);



            // Get the web where the event was raised

            SPWeb spCurrentSite = properties.OpenWeb();



            //Get the name of the list where the event was raised

            String curListName = properties.ListTitle;



            //If the list is our list named SubSites the create a new subsite directly below the current site

            if (curListName.ToLower() == "projects")
            {

                //Get the SPListItem object that raised the event

                SPListItem curItem = properties.ListItem;

                //Get the Title field from this item. This will be the name of our new subsite

                String curItemSiteName = properties.AfterProperties["Title"].ToString();

                // Get the rootweb where we will find the available templates

                SPWeb rootWeb = spCurrentSite.Site.RootWeb;

                //Get all the webtemplates available on this site collection

                SPWebTemplateCollection webTemplates = rootWeb.GetAvailableWebTemplates(1033);

                SPWebTemplate webTemplate = null;

                String webTemplateName = "NewProject"; //this is the name of the template we will use

                String webTemplateSearchName = "";


                //Get the template based on name. In this example 'ProjectTemplate'

                //Loop through the available templates
                for (int i = 0; i < webTemplates.Count; i++)
                {

                    webTemplateSearchName = webTemplates[i].Name.ToString();


                    if (webTemplateSearchName.Contains(webTemplateName))
                    {
                        //when we have found the matching template,set webTemplate to the foundt template and exit the if loop
                        webTemplate = webTemplates[webTemplateSearchName];

                        break;

                    }

                }

                //Create the subsite based on the template from the Solution Gallery

                SPWeb newSite = spCurrentSite.Webs.Add(curItemSiteName, curItemSiteName, "123", Convert.ToUInt16(1033), webTemplate, false, false);

                //Set the new subsite to inherit it's top navigation from the parent site, Use false if you do not want this.

                newSite.Navigation.UseShared = true;

                newSite.Close();





            }

        }


    }

Now as shown above, the event receiver will create a new sub-site when a list item is added. Now the above event receiver will work fine when users with "Full Control" Permission add an item (users who can create sub-sites), but if a user with Contribute permission adds a new list item , the event receiver will raise the following exception :-

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

when trying to create the new sub-site, since a contributor user can not create subsites. So can anyone advice if there is a way to force my event receiver to bypass the permission checking when creating a new sub-site?



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>