Friday, July 25, 2014

Access denied while the code accessed the property named MyWeb.Navigation.UseShared in SharePoint 2013

I don't know the reason why it took a long time for me to fix the issue in SharePoint 2013. I don't know whether my SharePoint developing skill is down or not and I think I am too old to work as a developer.

My situation is:
Step 1: Backup database of web application in SharePoint 2010
Step 2: Attach this database into SharePoint 2013
Step 3: Upgrade the site

Everything worked correctly accept for the home page throw the error "Access denied". They show "this page is not shared with you!"

The home is inherited the custom master page and using the custom code to add the navigation to top link bar.

This is a code:

            MyWeb.AllowUnsafeUpdates = true;

            if (!MyWeb.IsRootWeb)
                MyWeb.Navigation.UseShared = true;

                if (!MyWeb.ParentWeb.Navigation.UseShared)
                    // Get top link bar.
                    SPNavigationNodeCollection topnav = MyWeb.ParentWeb.Navigation.TopNavigationBar;

                    serverRelativeUrl = MyWeb.ServerRelativeUrl;

                    // Query for an existing link.
                    SPNavigationNode node = topnav
                        .FirstOrDefault(n => n.Url.Equals(serverRelativeUrl));

                    // No link, so add one.
                    if (node == null)
                        // Truncate long a title.
                        string linkTitle = MyWeb.Title;
                        if (linkTitle.Length > 15)
                            linkTitle = linkTitle.Substring(0, 12) + "...";

                        // Create the node.
                        node = new SPNavigationNode(linkTitle, serverRelativeUrl);

                        // Add it.
                        node = topnav.AddAsLast(node);

            MyWeb.AllowUnsafeUpdates = false;

You know what,  the property named "MyWeb.Navigation.UseShared" is only work with Design role assignment or higher. If I logged into site by contributor, it will throw exception "Access denied".

This is error tracking:

System.Threading.ThreadAbortException: Thread was being aborted.   at System.Threading.Thread.AbortInternal()   at System.Threading.Thread.Abort(Object stateInfo)   at System.Web.HttpResponse.AbortCurrentThread()   at Microsoft.SharePoint.Utilities.SPUtility.Redirect(String url, SPRedirectFlags flags, HttpContext context, String queryString)   at Microsoft.SharePoint.Utilities.SPUtility.RedirectToAccessDeniedPage(HttpContext context)   at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(HttpContext context)   at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex)   at Microsoft.SharePoint.SPSecurableObject.CheckPermissions(SPBasePermissions permissionMask)   at Microsoft.SharePoint.SPSecurity.ValidateSecurityOnOperation(SPOperationCode code, SPSecurableObject obj)   at Microsoft.SharePoint.SPSecurityOnOperationScope..ctor(SPOperationCode code, SPSecurableObject obj)   at Microsoft.SharePoint.Navigation.SPNavigation.set_UseShared(Boolean value)   at Bamboo.CommunityCentral.MasterPageBase.<AddCentralSiteToTopLinkBar>b__5()   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass5.<RunWithElevatedPrivileges>b__3()   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)   at Bamboo.CommunityCentral.MasterPageBase.AddCentralSiteToTopLinkBar()   at Bamboo.CommunityCentral.MasterPageBase.OnLoad(EventArgs e)

Remember: That code cannot work under privileged delegation, thus there is one way to pass the error is to check the current user before apply the code.

Good luck to you.

No comments: