Visit our SharePoint Forum

SharePoint developer? Submit Yourself as Freelancer

Tuesday, October 14, 2008

Change Links in the Quick Launch bar With User Control

Req : To Change the Url of a link named "Fedex" present in the quick launch bar from
Fedex US to Fedex canada... only if the user is from Canada. The user is identified with an ID field -Storeid, which can be pulled in from the QueryString. The ID's are checked against an SQL server database to reterive users(Stores) Country.

Approach: create a user control and register that control in the master page. Control will check the Id(storeid of the user logged in against an SQL server database and will change the link in quick Launch bar on fly.

Steps:

1. Create and UserControl in a asp.net webapplication and write the below code in it.
The code below
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace SPCustomWebPart
{
public partial class LeftNavControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string str = string.Empty;
string StoreId = string.Empty;
string CaFedexLink = string.Empty;
CaFedexLink =
http://www.fedex.com/ca_english/;

string USFedexLink = string.Empty;
USFedexLink =
http://www.fedex/us;

if(Request.QueryString["storeid"] != null)
{
StoreId = Request.QueryString["storeid"].ToString();
}
else
{
StoreId = null;
}

SqlDataReader DataReader = null;
String connStr = ConfigurationManager.ConnectionStrings["SQL_CONNECTION_NAME"].ConnectionString;
SqlConnection Connection = new SqlConnection(connStr);

SqlCommand SqlCommand = new SqlCommand("Select Country from dbo.Store_Info where Store_ID=" + StoreId , Connection);

try
{
Connection.Open();
DataReader = SqlCommand.ExecuteReader();

if (DataReader.HasRows)
{
while (DataReader.Read())
{
string Country = DataReader[0].ToString();

if (Country != null)
{
if (Country == "Canada")
{
ChangeLink(CaFedexLink);
}
else
{
ChangeLink(USFedexLink);
} } } }
}
catch
{ }
finally
{
Connection.Close();
}
}

private void ChangeLink(string NewLink)
{
try
{
SPSite site = SPContext.Current.Site;
SPWeb myWeb = site.RootWeb;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(site.ID))
{
using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
{
try
{
web.AllowUnsafeUpdates = true;
SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
foreach (SPNavigationNode node in nodes)
{
UpdateURL(node,web,NewLink);
}
}
catch
{ }
web.AllowUnsafeUpdates = false;
} }
});
}
catch
{ }
}
private void UpdateURL(SPNavigationNode Node,SPWeb web,string Link)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
web.AllowUnsafeUpdates = true;

if (Node.Children.Count > 0)
{
SPNavigationNodeCollection childNodes = Node.Children;
foreach (SPNavigationNode cNode in childNodes)
{
if (cNode.Title.ToString() == "Fedex")
{ cNode.Url = Link;
cNode.Update();
} } }
web.AllowUnsafeUpdates = false;
});
}
catch
{ }
} } }

2. Add the user Control in the Master Page using the
link here
3. If the Querystring is passed only to one page(main) the create a webPart on the main page to add the querytring to the session and change the above to be


if(Session["storeid"] != null)
{ StoreId = Session["storeid"].ToString(); }
else
{ StoreId = null; }



0 comments:

SharePoint Programming