2009年2月18日星期三

Sharepoint2007 - 自製Sitemap Webpart

小弟最近需要一個sitemap,但在Sharepoint中default的可以調控的選項太少,不太方便,所以直接寫了一個簡單的webpart去把左面navigation清楚的顯示出來。

如何建立webpart在這個post中已經教了大家,所以直接落去code的部份,究竟如何讀取navigation中的項目。

using Microsoft.SharePoint.Navigation;

// get current site info
SPWeb spweb = SPContext.Current.Web;
string spsiteurl = spweb.Url;

// get navigation
SPNavigation nav = spweb.Navigation;
SPNavigationNodeCollection nodeColl = nav.QuickLaunch;
foreach (SPNavigationNode node in nodeColl )
{
output.Write(node .Title+" - "+node .Url);
}

這是最簡單去表列所有node既方法,但是我們還需要管理權限的問題,讓用戶只能去到他所能看到的項目。所以加入了這個function去檢查用家的權限
// function to check if a user has the permission to read this node
private bool canReadNode(SPUser user, SPNavigationNode node)
{
string audience = "";

if (node.Properties["Audience"].ToString().StartsWith(";"))
{
audience = node.Properties["Audience"].ToString().Replace(";;;;", "");
}
else
{
audience = "";
}

if (audience.Equals(""))
{ return true; }
else
{
string[] stringSeparators = new string[] { "," };
String[] groupList = audience.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (String groupName in groupList)
{
if (bIsInGroup(user, groupName))
{
return true;
}
}
}
return false;
}
//function to check if an user is in a group
private bool bIsInGroup(SPUser user, string strname)
{
try
{
foreach (SPGroup group in user.Groups)
{
if (group.Name == strname)
return true;
}
}
catch (Exception exception)
{
WriteLogEvent(string.Format("An Error Occured | Exception Message:{0} StackTrace: {1}", exception.Message, exception.StackTrace));
}
return false;
}
其實SPNavigationNode還有其他的property如IsExternal與及Children等等,大家有興趣的可以繼續慢慢研究。

沒有留言:

發佈留言