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; }
Showing posts with label Master Pages(Look and Feel). Show all posts
Showing posts with label Master Pages(Look and Feel). Show all posts
Tuesday, October 14, 2008
Change Links in the Quick Launch bar With User Control
Tuesday, March 25, 2008
Useful Links for changing Look and Feel
Creating User control in master Page :
Add user control in sharepoint master page
Swtich Master Page according to logged in users group.
Switch SharePoint master page for logged in user
Tuesday, March 18, 2008
User control in Master Pages
Adding a user Contol in a master page is quite easy and is useful too. I have once added the control in the master page for changing the links in quick launch bar and once, for creating a custom footer in the master page .
I have added a post at my new website with detail screenshots which explains the procces to add a link in the master page with user control. Please see the link below.
Add user control in sharepoint master page
SharePoint Programming
Labels
- List and Libraries
- Master Pages(Look and Feel)
- Site Defination
- SP Interview Questions
- SP Knowledge Pack
- SP Query
- WebParts
- Workflows