Pages

Search This Blog

Showing posts with label navigation. Show all posts
Showing posts with label navigation. Show all posts

Wednesday, January 25, 2012

How to set Favicon to the SharePoint 2010 site

SharePoint 2010 comes with a orange Favicon and the image is present in following location
SharePoint Root Folder\Template\Images
[eg. C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES\].

Open this folder and search for favicon.ico and rename the file to FaviconBackup.ico.
Now copy your favicon to this folder and make sure it's named as "favicon.ico".
Now restart IIS(open Command Prompt -> IISReset), clear browser cache/temproary internet files.
Close the browser and reopen then browse the SharePoint site.

If mulitple sites are hosted in same farm and need to set different favicons for different sites then we have to update Master Page Code.
Open the site with SharePoint Designer 2010 and select Master pages -> v4.master. The default.master is still present in SharePoint 2010 for backward compatibility and visual upgrade.
But SharePoint 2010 uses v4.master only. Now click "Edit File" link to add the favicon code. If it asks for check out, click yes and continue.

Find the following line in the master page:


<SharePoint:SPShortcutIcon runat="server" IconUrl="/_layouts/images/favicon.ico" />


and change the path of favicon.
e.g.:
<SharePoint:SPShortcutIcon runat="server" IconUrl="/_layouts/images/brickred/favicon.ico" />


Save and close (If you have checked out then you need to check in and site collection administrator has to approve it via Site Settings -> Galleries -> Master pages and page layouts -> Check in and Approve).

Now reset IIS/clear browser cache and check.


Monday, December 5, 2011

Creating custom fly out menu : SharePoint 2010

Many a times, we need to add navigational elements in the default sharepoint navigation to allow the users to browse to our custom site pages/application pages. One good place for this is the sharepoint 'Site Actions' menu.

We can add custom fly out menus in the 'Site Actions' menu to allow the users to browse to our pages.

In order to do this, we need to create a class inheriting from the 'Control' class in the 'System.Web.UI.Controls' namespace.

In this class, we override the method 'CreateChildControls' and make use of the classes 'SubMenuTemplate' and 'MenuItemTemplate'in the 'Microsoft.SharePoint.WebControls' namespace to create menu items.

The code for the control is as follows:


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

namespace Blog_Test_Project.CustomControls
{
    public class CustomMenuControl:Control
    {
        string SubMenuIconUrl = @"/_layouts/images/menunewitem.gif";
        string MenuIconUrl = @"/_layouts/images/menunewitem.gif";
        string AppPage1Url = @"/_layouts/Blog_Test_Project/PostOnFacebook.aspx";
        string AppPage2Url = @"/_layouts/Blog_Test_Project/TypeAheadSearch.aspx";

        protected override void CreateChildControls()
        {
            SPWeb site = SPContext.Current.Web;
            // create fly out menu
            SubMenuTemplate smt = new SubMenuTemplate();
            smt.ID = "CustomSubMenu";
            smt.Text = "Test Project Menu Control";
            smt.Description = "Test Custom fly out menu";
            smt.MenuGroupId = 1;
            smt.Sequence = 1;
            smt.ImageUrl = site.Url + SubMenuIconUrl;
            // create fly out menu command 1
            MenuItemTemplate mit1 = new MenuItemTemplate();
            mit1.ID = "CustomFlyoutMenu1";
            mit1.Text = "Post on your facebook wall";
            mit1.Description = "This page is used to post on your facebook wall from a sharepoint page.";
            mit1.Sequence = 1;
            mit1.ClientOnClickNavigateUrl = site.Url + AppPage1Url;
            mit1.ImageUrl = site.Url + MenuIconUrl;
            // create fly out menu command 2
            MenuItemTemplate mit2 = new MenuItemTemplate();
            mit2.ID = "CustomFlyoutMenu2";
            mit2.Text = "Check out type ahead search in a document library";
            mit2.Description = "This page is used to do type ahead search in a document library.";
            mit2.Sequence = 2;
            mit2.ClientOnClickNavigateUrl = site.Url + AppPage2Url;
            mit2.ImageUrl = site.Url + MenuIconUrl;
            // add menu commands to Controls collection of fly out menu
            smt.Controls.Add(mit1);
            smt.Controls.Add(mit2);
            // add fly out menu to Controls collection of of menu control
            this.Controls.Add(smt);
        }
    }
}


Once this is done , we add an item of type 'Empty Element' in our sharepoint project:


and then we add the following 'CustomAction' element in the elements.xml file:



<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="TestCustomMenuControl"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1"
ControlAssembly="$SharePoint.Project.AssemblyFullName$"
ControlClass="Blog_Test_Project.CustomControls.CustomMenuControl"
Title="Custom Menu Control Test"
Description="Custom Menu Control Test"
/> 
</Elements>


After adding the element, we also need to add a safecontrol entry for this class since it is a custom control.We can do this by clicking on the Element in Visual Studio, clicking on 'SafeControls' in its property sheet and clicking on 'Add' to create a new entry:




Once this is done, we can deploy the solution by clicking on 'Deploy' for the sharepoint project in Visual Studio.



Once the solution is deployed, we can see the results by going to the site and opening up the 'Site Actions' menu.



Hope this helps!!

Monday, February 28, 2011

Create custom navigation provider

Hi,

Sometimes we need to overwrite the existing navigation provider and write our own provider to get the data from list,db or any other third party application.This post will help you to understand how you can implement the custom navigation provider.

The implementation requires the following steps

1.) Creation of new class which should be inherited from "PortalSiteMapProvider" class.
2.) Custom provider entry in web.config file
3.) Custom provider reference in master page or user control


Create new Class inherited from "PortalSiteMapProvider"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Publishing.Navigation;
using Microsoft.SharePoint.Publishing;
using System.Web;
using Microsoft.SharePoint;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;
using System.Collections;

namespace XYZ.XYZ
{
//Assign the neccessary security permissions
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class XYZNavigationProvider : PortalSiteMapProvider
{
//Create the in memory objects for storage and fast retreival
protected SiteMapNodeCollection siteMapNodeColl;
protected ArrayList childParentRelationship;

//These are only the top level nodes that will show in the top nav
protected ArrayList topLevelNodes;
private PortalSiteMapNode rootNode = null;
SiteMapNodeCollection objNodesCollection;

/// Override the initialize method of the superclass
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
try
{
lock (this)
{
if (rootNode != null)
{
return;
}
else
{
// Verify that config isn't null
if (config == null)
throw new ArgumentNullException("config is null");

// Assign the provider a default name if it doesn't have one
if (String.IsNullOrEmpty(name))
name = Constants.NAVIGATION_PROVIDER;

// Add a default "description" attribute to config if the
// attribute doesn’t exist or is empty
if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "Custom navigation provider");
}

NavigationArea = Convert.ToString(config["description"]);

base.Initialize(name, config);
}
}
}
catch (Exception ex)
{
throw(ex);
}

}

public override SiteMapNode CurrentNode
{
get
{
string currentUrl = FindCurrentUrl();
// Find the SiteMapNode that represents the current page.
SiteMapNode currentNode = FindSiteMapNode(currentUrl);

if (currentNode == null)
{
return base.CurrentNode;
}

return currentNode;
}
}

public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode node)
{
PortalWebSiteMapNode ptlWebSiteMapNode;
SiteMapNodeCollection objSiteMapNodeCollection = default(SiteMapNodeCollection);
try
{
PortalSiteMapNode portalNode = node as PortalSiteMapNode;

if (portalNode != null && portalNode is PortalWebSiteMapNode)
{
ptlWebSiteMapNode = (PortalWebSiteMapNode)portalNode;

if (portalNode.Type == NodeTypes.Area)
{
objSiteMapNodeCollection = ComposeNodes(node, (PortalWebSiteMapNode)portalNode);
}
else
{
objSiteMapNodeCollection = base.GetChildNodes(portalNode);
}
}
else
{
objSiteMapNodeCollection = new SiteMapNodeCollection();
}

}
catch (Exception ex)
{
throw(ex);
}
objNodesCollection = objSiteMapNodeCollection;
return objSiteMapNodeCollection;

}

public virtual SiteMapNodeCollection ComposeNodes(System.Web.SiteMapNode node, PortalWebSiteMapNode ptlWebSiteMapNode)
{
object Nodes;
PortalSiteMapNode RootNode;
SPQuery objSPQuery;
String Query;
SPListItemCollection objItems;
String PageURL;
String Title = String.Empty;
SiteMapNodeCollection nodeColl = new SiteMapNodeCollection();
SPSite currentSite;
SPWeb currentWeb;
SPList objList;
bool isChangePasswordVisible = true;
//The SiteMapNodeCollection which represents the children of this node
SiteMapNodeCollection children = new SiteMapNodeCollection();
PortalSiteMapNode portalNode = node as PortalSiteMapNode;
bool IsNavigationLinkEnable;



if (Nodes != null && Nodes is SiteMapNodeCollection)
{
return ((SiteMapNodeCollection)Nodes);
}



nodeColl = new SiteMapNodeCollection();
currentSite = SPContext.Current.Site;
currentWeb = currentSite.RootWeb;
objList = currentWeb.Lists[Constants.LIST_NAVIGATIONS];

objSPQuery = new SPQuery();
Query = String.Format("{0}", NavigationArea);
objSPQuery.Query = Query;

objItems = objList.GetItems(objSPQuery);

if (objItems != null && objItems.Count > 0)
{
foreach (SPListItem objItem in objItems)
{


Title = objItem[SPBuiltInFieldId.Title]);


if (!String.IsNullOrEmpty(PageURL))
{
RootNode = new PortalSiteMapNode(ptlWebSiteMapNode, Title, NodeTypes.Custom, PageURL, Title, Title);
}
else
{
RootNode = new PortalSiteMapNode(ptlWebSiteMapNode, Title, NodeTypes.Custom, "#", Title, Title);
}

// Call method to get the child items
GetChildNavigations(objItem.Title, RootNode, objList, ptlWebSiteMapNode);


nodeColl.Add(RootNode);
}
}

}


return nodeColl;
}

void GetChildNavigations(string KeyTitle, PortalSiteMapNode RootNode, SPList objList,PortalWebSiteMapNode ptlWebSiteMapNode)
{
SPQuery objSPQuery;
SPListItemCollection objItems;
PortalSiteMapNode childNode;
bool isChangePasswordVisible = true;
bool IsNavigationLinkEnable;

SiteMapNodeCollection objSiteMapNodeCollection = new SiteMapNodeCollection();
objSPQuery = new SPQuery();
String Title =String.Empty;
String PageURL;

//objSPQuery.Query = String.Format("{0}", Title);
objSPQuery.Query = String.Format("{0}", KeyTitle);

objItems = objList.GetItems(objSPQuery);

if (objItems != null && objItems.Count > 0)
{
foreach (SPListItem objItem in objItems)
{
Title = Convert.ToString(objItem[SPBuiltInFieldId.Title]);

if (!String.IsNullOrEmpty(PageURL))
{
childNode = new PortalSiteMapNode(ptlWebSiteMapNode, Title, NodeTypes.Custom, PageURL, Title, Title);
}
else
{
childNode = new PortalSiteMapNode(ptlWebSiteMapNode, Title, NodeTypes.Custom, "#", Title, Title);
}

objSiteMapNodeCollection.Add(childNode);
// Call method again (recursion) to get the child items
GetChildNavigations(objItem.Title, childNode, objList, ptlWebSiteMapNode);
}
}
}

RootNode.ChildNodes = objSiteMapNodeCollection;
}
else
{
return;
}
}




#region Supporting Methods
// Get the URL of the currently displayed page.
private string FindCurrentUrl()
{
try
{
// The current HttpContext.
HttpContext currentContext = HttpContext.Current;
if (currentContext != null)
{
return currentContext.Request.RawUrl;
}
else
{
throw new Exception("HttpContext.Current is Invalid");
}
}
catch (Exception e)
{
throw new NotSupportedException("This provider requires a valid context.", e);
}
}

// Implement the FindSiteMapNode method.
public override SiteMapNode FindSiteMapNode(string rawUrl)
{

// Does the root node match the URL?
if (RootNode.Url == rawUrl)
{
return RootNode;
}
else
{
SiteMapNode candidate = null;
// Retrieve the SiteMapNode that matches the URL.
lock (this)
{
candidate = GetNode(objNodesCollection, rawUrl);
}
return candidate;
}
}

private SiteMapNode GetNode(SiteMapNodeCollection objNodesCollection, string url)
{
SiteMapNode objInnerNode = null;

if (objNodesCollection != null)
{
foreach (SiteMapNode objNode in objNodesCollection)
{
if (objNode.Url == HttpContext.Current.Request.Url.AbsoluteUri)
return objNode;
else if (objNode.HasChildNodes && objNode.ChildNodes.Count > 0)
{
objInnerNode = GetNode(objNode.ChildNodes, url);
if (objInnerNode != null)
{
return objInnerNode;
}
}
}
}
return null;
}

#endregion
}
}

Add entry in web.config

After creating the class, add the entry of the class in web.config file of the site.
Add this entry under the sitemap->provider tag.

< siteMap defaultProvider="CurrentNavigation" enabled="true" >
< providers >
< add name="XYZNavigationProviderTop" description="Custom Provider" type="XYZ.XYZNavigationProvider, XYZ" NavigationType="Current" / >
< /provider >
< /sitemap >

Use the custom navigation provider in the master page

< publishingnavigation:portalsitemapdatasource sitemapprovider="XYZNavigationProviderLeft"
id="XYZNavSiteMap" runat="server" showstartingnode="false"/ >


Friday, February 18, 2011

Creating User Custom Actions through Code

In one of our projects, the requirement was to create an ECB menu item on list only if its configured by administrator from a custom web part (a custom web part with check boxes to determine whether an ECB is required or not). We accomplished it through custom code using client object modal.
by creating an object of UserCustomAction and assigning properties to it.
The UserCustomAction property returns the collection of custom actions for a site collection, Web site, list ot content Types. So this means that you can assign custom action to any of these.

In the below example, I am adding a user custom action for list items.
To place the new action on the menu (the one which is displayed when you hover on a list item and click to view the ECB), the Location property specifies EditControlBlock (ECB), Sequence specifies the order of placement in relation to other user custom actions, and Url specifies an absolute path to a page that defines the action.

using System;
using Microsoft.SharePoint.Client;
namespace MyCustomAction
{
    class CreateUserCustomActionList
    {
        static void Main()
        {
            String strSiteUrl = "http://mysharepointsite/";
            ClientContext clientContext = new ClientContext(strSiteUrl);
            Web objWeb = clientContext.Web;
            List objList = objWeb.Lists.GetByTitle("Demo List");
            UserCustomActionCollection collUserCustomAction = objList .UserCustomActions;
            UserCustomAction objUserCustomAction= collUserCustomAction.Add();
            objUserCustomAction.Location = "EditControlBlock";
            objUserCustomAction.Sequence = 100;
            objUserCustomAction.Title = "Demo User Custom Action";
            objUserCustomAction.Url = strSiteUrl + @"/_layouts/DemoPage.aspx";
            objUserCustomAction.Update();
            clientContext.Load(objList,
                list => list.UserCustomActions);
            clientContext.ExecuteQuery();
        }
    }
}

Tuesday, December 21, 2010

Context sensitive ECB menu items in sharepoint using javascript.

This is a nice post showing how to build custom ECB menu items that are context sensitive i.e. menus that change according to the metadata of the document.

Sharepoint changes the ECB menu for different states of the document . For eg. when document is checked out, checked in etc. This post provides insight into how to implement similar kind of functionality for custom menu items:

http://weblogs.asp.net/jan/archive/2009/09/11/customizing-the-sharepoint-ecb-with-javascript-part-3.aspx