Showing posts with label SharePoint 2007. Show all posts
Showing posts with label SharePoint 2007. Show all posts

Sunday, February 13, 2011

Customizing the Quick Launch menu: Adding fly-out menus to SharePoint navigation


In this post, I’ll show you how to customize the Quick Launch menu to display several levels of data in a dynamic way and use this customized menu for quick access to all Views within a List without consuming space on the Quick Launch.
First, let’s add a List and make sure it shows on the Quick Launch. Let’s call this list “Navigation Test List”, and then add 4 Views to the list.
Next, let’s write some OM code that, when run, adds a link to each of the List’s Views under the List Link on the Quick Launch. Add the following code to a new C# Console Application in Visual Studio .NET or 2005 (and don’t forget to add a reference to Microsoft.Sharepoint.dll).
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://server");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["Navigation Test List"];
            SPNavigationNode rootListLink = web.Navigation.GetNodeByUrl(list.DefaultViewUrl);
            SPNavigationNode node = null;
            foreach (SPView view in list.Views)
            {
                node = new SPNavigationNode(view.Title, view.Url, false);
                rootListLink.Children.AddAsFirst(node);
            }
        }
    }
}

At this point, we have links to all of the Views under the List, but they cannot be displayed since the menu control ignores the links after the second level. So, let’s modify the menu control to display what we want. Perform the following to accomplish this task:

1.     Navigate to the master page gallery: From the home page click on Site Actions, then Site Settings, and then on Master Pages, under the Galleries column
2.     Click on the drop down menu for the master page you want to modify, and then click on Edit in Microsoft Office SharePoint Designer
3.     Locate the Quick Launch Menu control, and modify the StaticDisplayLevels and MaximumDynamicDisplayLevels attributes:
<SharePoint:AspMenu
id="QuickLaunchMenu"
DataSourceId="QuickLaunchSiteMap"
runat="server"
Orientation="Vertical"
StaticDisplayLevels="2"
ItemWrap="true"
MaximumDynamicDisplayLevels="1"
StaticSubMenuIndent="0"
SkipLinkText="">
4.     Save your changes and reload the page from the browser. Hover over the Links on the Quick Launch. The end result should look like this:

5.     Optional: Modify other properties in the menu control to match the look and feel of your site. The above steps can also be applied to the Top Link Bar.

Friday, November 26, 2010

Setting Permissions on SharePoint Views

 
Heres a little hack that you can do, in order to create permissions on views of any list or library.  I’m not going to say this is the most pretty or perfect solution, but it’s at least a way to accomplish this.  It gets asked all the time.  “How can I set permissions so that only certain people can see certain views of my list?” 

So the next option is a non-supported approach, but my favorite: use HTTP modules!

Http modules intercept page requests and allow custom code to run before the page actually renders. Thus, we can intercept the request and check if it is an application page. Then we can change the master page reference dynamically as the page renders.

To achieve this, follow these steps:
1.       Create a new VS project (Class Library project) and call it HttpModule
2.       Rename Class1 to MyHttpModule and add the following code to it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;
using Microsoft.SharePoint;

namespace HttpModule
{
    public class MyHttpModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(validation_PreRequestHandlerExecute);
        }

         void validation_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                page.PreInit += new EventHandler(validation_PreInit);
            }
        }
        void validation_PreInit(object sender, EventArgs e) {
            Page page = sender as Page;
            if (page != null) {
                string url = page.Request.Url.ToString ();
                if (url.Contains("/Lists/wjc/summary.aspx"))// Lists/wjc/summary.aspx is the relative url of list view.
                {
                    if (HttpContext.Current.User.Identity.Name != "MICRO\\porter")//MICRO\\porter is login name.
                    {
                        page.Response.Redirect("/_layouts/accessdenied.aspx");
                    }
                }
                if (url.Contains("/_layouts/accessdenied.aspx")&&page.Request .UrlReferrer .ToString ().Contains ("/Lists/wjc/summary.aspx")) {
                    if (HttpContext.Current.User.Identity.Name == "MICRO\\porter")
                    {
                        page.Response.Redirect("/Lists/wjc/summary.aspx");
                    }
                }
            }
        }
        public void Dispose()
        {
        }
    }
}


1.       Sign and build the project, then place the dll in the GAC
2.       Go to your site’s virtual directory location (usually at c:\Inetpub\wwroot\wss\VirtualDirectories\SITENAME)
3.       Add the next line to your web.config within the “httpModules” section:
<add name=”HttpModule” type=”HttpModule.MyHttpModule, HttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ca0f662d5d7f9d5d″ />

Hope this helps!