The Source property can be appended onto your URL’s to ensure that users are redirected back to a specific location after completing an action. For example if you have a link that goes to add or edit an item in a SharePoint list you can append Source=[URL] to ensure the user is redirected back to the page with the link once they select ok or cancel.
How to create your own Cascading Navigation using the ASPMenu control
August 20, 2007Overview: The following code demonstrates how to create a custom menu control in SharePoint that goes 3 levels deep with the same look and feel as the SharePoint horizontal naviagation.
Prerequisites: A custom list to maintain the data for the menu items must be created with the following columns
· Title – Single line of text
· Link URL – Single line of text
· Link ID – Single line of text
· Parent ID – Single line of text
· Link Order – Number
· Display – Choice {Yes, No]
· Item Level – Choice [Level 1, Level 2, Level 3]
Code:
1. Set up a web part property to get and set the admin list created above:
[Personalizable, WebBrowsable, WebDisplayName("Admin List")]
public string Set_AdminList
{get {
return _AdminList;
}
set
{
_AdminList = value;
}
}
2. Create the ASP Menu control and set the look and feel
AspMenu _menu;
_menu = new AspMenu();
_menu.ID = “CustomMenu”;
_menu.EnableViewState = false;
_menu.Orientation = System.Web.UI.WebControls.Orientation.Horizontal;
_menu.StaticDisplayLevels = 1;
_menu.MaximumDynamicDisplayLevels = 3;
_menu.StaticSubMenuIndent = 0;
_menu.DynamicHorizontalOffset = 0;
_menu.StaticPopOutImageUrl = “/_layouts/images/menudark.gif”;
_menu.StaticPopOutImageTextFormatString = “”;
_menu.DynamicHoverStyle.BackColor = Color.FromName(“#CBE3F0″);
_menu.SkipLinkText = “”;
_menu.StaticSubMenuIndent = 0;
_menu.CssClass = “ms-topNavContainer”;
_menu.StaticMenuItemStyle.CssClass = “ms-topnav”;
_menu.StaticMenuItemStyle.ItemSpacing = 0;
_menu.StaticSelectedStyle.CssClass = “ms-topnavselected”;
_menu.StaticHoverStyle.CssClass = “ms-topNavHover”;
_menu.DynamicMenuStyle.BackColor = Color.FromName(“#F2F3F4″);
_menu.DynamicMenuStyle.BorderColor = Color.FromName(“#A7B4CE”);
_menu.DynamicMenuStyle.BorderWidth = 1;
_menu.DynamicHoverStyle.BackColor = Color.FromName(“#CBE3F0″);
_menu.DynamicHoverStyle.BorderColor = Color.FromName(“#CBE3F0″);
_menu.DynamicMenuItemStyle.CssClass = “ms-topNavFlyOuts”;
_menu.DynamicHoverStyle.CssClass = “ms-topNavFlyOutsHover”;
_menu.DynamicSelectedStyle.CssClass = “ms-topNavFlyOutsSelected”;
3. Loop through the list and build out the list
SPWeb thisWeb = null;
try
{
SPSite thisSite = SPControl.GetContextSite(Context);
thisWeb = thisSite.OpenWeb(“/”);
SPList spCascadingNav = thisWeb.Lists[_AdminList];
SPQuery query = new SPQuery();
query.Query = “<OrderBy><FieldRef Name=’Link_x0020_Order’ /></OrderBy><Where><And><Eq><FieldRef Name=’Item_x0020_Level’ /><Value Type=’Choice’>Level 1</Value></Eq><Eq><FieldRef Name=’Display’ /><Value Type=’Choice’>Yes</Value></Eq></And></Where>”;
SPListItemCollection items = spCascadingNav.GetItems(query);
SPQuery level2query = new SPQuery();
level2query.Query = “<OrderBy><FieldRef Name=’Link_x0020_Order’ /></OrderBy><Where><And><Eq><FieldRef Name=’Item_x0020_Level’ /><Value Type=’Choice’>Level 2</Value></Eq><Eq><FieldRef Name=’Display’ /><Value Type=’Choice’>Yes</Value></Eq></And></Where>”;
SPListItemCollection level2items = spCascadingNav.GetItems(level2query);
SPQuery level3query = new SPQuery();
level3query.Query = “<OrderBy><FieldRef Name=’Link_x0020_Order’ /></OrderBy><Where><And><Eq><FieldRef Name=’Item_x0020_Level’ /><Value Type=’Choice’>Level 3</Value></Eq><Eq><FieldRef Name=’Display’ /><Value Type=’Choice’>Yes</Value></Eq></And></Where>”;
SPListItemCollection level3items = spCascadingNav.GetItems(level3query);
//For each item, generate the row.
System.Web.UI.WebControls.MenuItem _item = new System.Web.UI.WebControls.MenuItem();
System.Web.UI.WebControls.MenuItem _item2;
System.Web.UI.WebControls.MenuItem _item3;
foreach (SPListItem menuItem in items)
{
string parentID = ” “;
if (menuItem["Parent ID"] != null)
{
parentID = menuItem["Parent ID"].ToString();
}
else { parentID = “None”; }
string title = menuItem["Title"].ToString();
string url = menuItem["Link URL"].ToString();
string itemID = menuItem["Link ID"].ToString();
_item = new System.Web.UI.WebControls.MenuItem(title, “”, “”, url);
foreach (SPListItem subMenuItem in level2items)
{
string subparentID = ” “;
if (subMenuItem["Parent ID"] != null)
{ subparentID = subMenuItem["Parent ID"].ToString();}
else { subparentID = “None”; }
if (subparentID == itemID)
{
string subtitle = subMenuItem["Title"].ToString();
string suburl = subMenuItem["Link URL"].ToString();
string subID = subMenuItem["Link ID"].ToString();
_item2 = new System.Web.UI.WebControls.MenuItem(subtitle, “”, “”, suburl);
foreach (SPListItem sub3MenuItem in level3items)
{
string sub3parentID = ” “;
if (sub3MenuItem["Parent ID"] != null)
{sub3parentID = sub3MenuItem["Parent ID"].ToString();}
else { sub3parentID = “None”; }
if (sub3parentID == subID)
{
string sub3title = sub3MenuItem["Title"].ToString();
string sub3url = sub3MenuItem["Link URL"].ToString();
_item3 = new System.Web.UI.WebControls.MenuItem(sub3title, “”, “”, sub3url);
_item2.ChildItems.Add(_item3);}
}
_item.ChildItems.Add(_item2);
}
}
_menu.Items.Add(_item);
Controls.Add(_menu);
}
}
catch (Exception ex)
{
Controls.Add(new LiteralControl(“An error has occured with this web part. Please contact your system administrator and relay this error message: ” + ex.Message));
}
finally
{
if (thisWeb != null)
thisWeb.Dispose();
}
}
Disposing of SharePoint Services Objects
August 19, 2007When using the SharePoint Services objects make sure to dispose of them properly since some of them, such as the SPSite class and SPWeb class objects, use unmanaged code. Here is a reference for how and when to properly dispose:
http://msdn2.microsoft.com/en-us/library/aa973248.aspx
The common objects used are SPSite and SPWeb which should be disposed of.
Note that the SPSite object if obtained from the GetContextSite() method does not need to be disposed of.
Posted by Yvonne Harryman