Click here to Skip to main content
13,254,754 members (52,461 online)
Click here to Skip to main content
Add your own
alternative version

Stats

14K views
318 downloads
2 bookmarked
Posted 16 Mar 2013

Sharepoint Tree View Sites Navigation

, 16 Mar 2013
Rate this:
Please Sign up or sign in to vote.
This WebPart is developed to display all site collections and subsites based on logged in authentication

Introduction

In this tip, I provide a Sharepoint webpart solution developed using Visual Studio 2010. It contains a Sharepoint Treeview webpart that displays site collections and subsites based on permissions applied on these sites.

When a user is logged in to the page, he is going to view the sites and subsites of all site collections - that he has permission on it - in one place then when the user clicks on one of the titles, he will be navigated to the site in a new page.

Main Requirements

  • The Tree View will display the sites titles based on permission of the logged in user.
  • A user with Permission from Read only to Administration can click on the Title of the Site to navigate
  • The site will be displayed in a new window.
  • A user with no permission on a specific site will not be able to see the link.
  • No configurations are required, can be added to any site at any level in a specific webapplication.

ScreenShot

Deployment

  • The solution will be deployed on site level (scope)
  • A user control will be added in 14% in this location:
    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\TreeSitesViwr\SitesViwrWP\ SitesViwrWPUserControl.ascx
  • A solution Assembly DLL will be deployed in the GAC (TreeSitesViwr.dll) C:\windows\assembly\GAC_msil\TreeSitesViwr\1.0.0.0__19d8525006072941
  • A resource file will be added in the App_GlobalResources (SitesViwrRes .resx).
  • 2 DLLs will be added in the bin folder of the web application (Telerik.Web.Design.dll) and (Telerik.Web.UI.dll)
  • Source control will also be added to the web.config file.
    <SafeControl Assembly="TreeSitesViwr, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=19d8525006072941" Namespace="TreeSitesViwr.SitesViwrWP" 
    
    TypeName="*" Safe="True" SafeAgainstScript="False" /> 
  • Feature name is TreeSitesViwr Feature

Solution

  • WSP file (TreeSitesViwr.wsp) (Download SitesViwrWP)
  • Visual WebPart contains user control and code behind (Download TreeSitesViwr)

Code

First UserControl Design:

  • Register Telerik Assemblies:
    <%--for telerik only--%>
    <%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
    <%@ Register assembly="Telerik.Web.Design" namespace="Telerik.Web.UI.Design" 
    
    tagprefix="telerik" %>  

    Add RadTreeView control and define its properties:

    <telerik:RadTreeView runat="server" ID="SiteTV" BackColor="Silver" 
    
    BorderColor="#660033" BorderStyle="Solid" ForeColor="Black" Width="300px">
    </telerik:RadTreeView> 

In the code behind, add these functions:

  • In the Page_Load:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BuildSiteTree();//call this function to build the tree View     
        }
    
        SiteTV.CollapseAllNodes();// by default all nodes are Collapse
    } 
  • Add this function BuildSiteTree( ):
    private void BuildSiteTree()
    {
        int CountSites = 0;
        int Count_No_perm = 0;
        
        // Get all Site Collections
        
        SPWebApplication WebApp = SPContext.Current.Web.Site.WebApplication;
        SPSiteCollection TScoll = WebApp.Sites;
        
        //to loop through all site collections with sys privilege 
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            try
            {
                foreach (SPSite TSite in TScoll)
                {
                    try
                    {
                        //All site collections except the Default (Root)
                        if (CountSites != 0)
                        {
                            //Display RootNodes with permission
                            bool hasperm = UserHasPerm(TSite.Url.ToString());
                            RadTreeNode rootNode = new RadTreeNode
                            	(TSite.RootWeb.Title, TSite.Url);
                            rootNode.NavigateUrl = TSite.Url;
                            rootNode.Target = "_blank";
                       
                            if (UserHasPerm(TSite.Url.ToString()))
                            {
                                SiteTV.Nodes.Add(rootNode);
                            }
                            using (SPWeb web = TSite.OpenWeb())
                            {
                                try
                                {
                                    foreach (SPWeb subSite in web.Webs)
                                    {
                                        if (UserHasPerm(subSite.Url.ToString()))
                                        {
                                            try
                                            {
                                                AddNodes(subSite, rootNode.Nodes);
                                                Count_No_perm = Count_No_perm + 1;
                                            }
                                            catch
                                            {
                                                throw;
                                            }
                                            
                                            finally
                                            {
                                                if (subSite != null)
                                                    subSite.Dispose();
                                            }
                                        }
                                    }
                                }
                                
                                //if problem in accessing subsites just move to the next 
                                catch
                                {
                                    continue;
                                }
                            }
                        }
                        
                        CountSites = CountSites + 1;
                    }
                    
                        //if problem in accessing certain site collection just move to the next 
                    catch
                    {
                        continue;
                    }
                }
                //if there was no access on any site collections
                
                if (Count_No_perm == 0)
                {
                    WPpnl.Visible = false;
                    lblError.Visible = true;
                    lblError.Text = HttpContext.GetGlobalResourceObject
                    	("SitesViwrRes", "NoSites").ToString();
                }
            }
            //problem with site collections level
            catch 
            {
                throw;
            }
        });
    }
  • Also add this function UserHasPerm( ) for authentication check and it will return bool value T if the logged in user has permission and F if not:
    private bool UserHasPerm(string TSite)
    {
         SPWeb web = new SPSite(TSite).OpenWeb();
         bool hasperm = web.DoesUserHavePermissions
                  (SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.ViewPages);
         return hasperm;
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AfnanMof
Saudi Arabia Saudi Arabia
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionNot an article Pin
OriginalGriff15-Mar-13 22:59
mvpOriginalGriff15-Mar-13 22:59 
If you take away the code, there is pretty much nothing left!
Have you considered posting it as a Tip instead?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

AnswerRe: Not an article Pin
AfnanMof15-Mar-13 23:48
memberAfnanMof15-Mar-13 23:48 
GeneralRe: Not an article Pin
OriginalGriff15-Mar-13 23:58
mvpOriginalGriff15-Mar-13 23:58 
GeneralRe: Not an article Pin
AfnanMof16-Mar-13 0:04
memberAfnanMof16-Mar-13 0:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 16 Mar 2013
Article Copyright 2013 by AfnanMof
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid