Friday, December 26, 2008

Programmatically customize site navigation

The SPWeb object has been enhanced in MOSS 2007 to support the ability to control the navigation of a SharePoint site programmatically!

The SPWeb object has a new property named Navigation that returns a SPNavigation object. This object allows developers to programmatically control the navigation of a SharePoint site.

Here is how you add a menu item to the QuickLaunch navigation menu.

These QuickLaunch examples assume you have created a top level site named quicklaunch.

Once this top level site has been created its URL will look like this: http://komal/quicklaunch
In this example we will add two links to the QuickLaunch menu for the quicklaunch top level site, one link will be internal and one will be external. The internal link will point to the Links list in the QuickLaunch site. The external link will point to the SharePoint Experts web site.

SPSite quickLaunchSite = new SPSite(“http://komal/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

SPNavigationNode internalMenuItem = new SPNavigationNode(“Links”, “Lists/Links/AllItems.aspx”, false);

quickLaunchNodes.AddAsFirst(internalMenuItem);

SPNavigationNode externalMenuItem = new SPNavigationNode(“SharePoint Data”, “http://SharePointdata.blogspot.com”, true);

quickLaunchNodes.AddAsFirst(externalMenuItem);

quickLaunchWeb.Update();

Here is how you remove a menu item from the QuickLaunch navigation menu.

In this example we will remove the external link to the SharePoint Experts web site.

SPSite quickLaunchSite = new SPSite(“http://komal/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

quickLaunchNodes.Delete(quickLaunchNodes([0]));

quickLaunchWeb.Update();

Here is how you add grouped menu items to the QuickLaunch navigation menu.

In this example we will create a header link for the group of links and name it Administration. Then we will add two links under the Administration header link. The links will link to the Create and Site Settings pages for the quicklaunch Site Collection.

SPSite quickLaunchSite = new SPSite("http://" + serverTextBox.Text + "/quicklaunch");

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

SPNavigationNode internalMenuItem = new SPNavigationNode("Administration", "", false);

quickLaunchNodes.AddAsFirst(internalMenuItem);

SPNavigationNode externalSubMenuItem1 = new SPNavigationNode("Site Settings", quickLaunchWeb.Url + "/_layouts/settings.aspx", true);

quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem1);

SPNavigationNode externalSubMenuItem2 = new SPNavigationNode("Create", quickLaunchWeb.Url + "/_layouts/create.aspx", true);

quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem2);

quickLaunchWeb.Update();

Top Navigation Menu Items

The top navigation menu may also be accessed programmatically. You can create new menu items in the top navigation menu navigation menu and remove them. You can also specify if the link is external to the site.

Here is how you add a menu item to the top navigation menu.

This example assumes you have created a top level site named topnavigation.

Once this Site Collection has been created its URLs will look like this: http://komal/topnavigation

In this example we will add two links to the top navigation menu for the topnavigation top level site, one link will be internal and one will be external. The internal link will point to the Links list in the topnavigation site. The external link will point to the SharePoint Experts web site.

SPSite topNavigationSite = new SPSite(“http://komal/topnavigation”);

SPWeb topNavigationWeb = topNavigationSite.OpenWeb();

SPNavigationNodeCollection topNavigationNodes = topNavigationWeb.Navigation.TopNavigationBar;

SPNavigationNode internalMenuItem = new SPNavigationNode(“Links”, “Lists/Links/AllItems.aspx”, false);

topNavigationNodes.AddAsFirst(internalMenuItem);

SPNavigationNode externalMenuItem = new SPNavigationNode(“SharePoint Data”, “http://SharePointdata.blogspot.com”, true);

topNavigationNodes.AddAsFirst(externalMenuItem);

topNavigationWeb.Update();



Wednesday, December 10, 2008

GetListItem using sharepoint webservice

public System.Data.DataSet GetListItem(string UserName, string Password, string Domain, string SiteURL, string ListName, string nodeQuery, Guid SiteGuid)

{

DataSet dsList = new DataSet();

try

{

ListData.Lists list = new ListData.Lists();

list.Url = SiteURL + "/_vti_bin/lists.asmx";

list.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);

list.PreAuthenticate = true;

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlNode ndQuery = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "Query", "");

//ndQuery.InnerXml = "";

ndQuery.InnerXml =nodeQuery;

System.Xml.XmlNode ndViewFields = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "ViewFields", "");

System.Xml.XmlNode ndQueryOptions = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "QueryOptions", "");

System.Xml.XmlNode items = list.GetListItems(ListName, string.Empty, ndQuery, ndViewFields, "", ndQueryOptions, SiteGuid.ToString());

System.IO.StringReader sr = new System.IO.StringReader(items.OuterXml.ToString());

dsList.ReadXml(sr);

return dsList;

}

catch (Exception)

{

}

return dsList;

}

Get SitID/WebID using sharepoint webservice

public Guid GetSiteID(string UserName, string Password, string Domain, string SiteURL)
{

Guid SiteGuid = Guid.Empty;

try

{

SiteData.SiteData site = new TP.WebService.SiteData.SiteData();

site.Url = SiteURL + "/_vti_bin/sitedata.asmx";

site.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);

site.PreAuthenticate = true;

SiteData._sWebMetadata webMetaData;

SiteData._sWebWithTime[] arrWebWithTime;

SiteData._sListWithTime[] arrListWithTime;

SiteData._sFPUrl[] arrUrls;

string roles; string[] roleUsers; string[] roleGroups;

uint i = site.GetWeb(out webMetaData, out arrWebWithTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups);

SiteGuid = new Guid(webMetaData.WebID);

}

catch (Exception)

{

}

return SiteGuid;

}

Using Sharepoint Webservice to add/update/delete list Item

Using Sharepoint webservice, i have insert/update/delete list item through lists.asmx.
Using this code,just pass necessary parameter to run this code.

For Insert Item Query:

string caml="<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">New</Field>" + "<Field Name=\"Title\">" + TextBox1.Text.ToString() + "</Field>" + "</Method>";

For Update Item Query:

string caml="<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">1</Field>" + "<Field Name=\"Title\">" + TextBox1.Text.ToString() + "</Field> + "</Method>";

For Delete Item Query :

string caml="<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">1</Field></Method>";

TP.WebService.ListOperation listItemAdd = new TP.WebService.ListOperation();

listItemAdd.InsertUpdateDeleteListItem("komal", "admin", komal, "http://komal:1222", caml, "test1");


public bool InsertUpdateDeleteListItem(string UserName, string Password, string Domain, string SiteURL,string CAML,string ListName)
{
try
{

ListData.Lists list = new ListData.Lists();
list.Url = SiteURL +"/_vti_bin/lists.asmx";
list.Credentials = new System.Net.NetworkCredential(UserName,Password,Domain);
System.Xml.XmlDocument xmlDoc;
System.Xml.XmlElement elBatch;
System.Xml.XmlNode ndReturn;
xmlDoc = new System.Xml.XmlDocument();
elBatch = xmlDoc.CreateElement("Batch");
elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
string strBatch = CAML;
elBatch.InnerXml = strBatch;
ndReturn = list.UpdateListItems(ListName, elBatch);
return true;

}

catch (Exception)
{
return false;
}
return true;
}

Friday, December 5, 2008

SharePoint Custom Action Identifiers

http://johnholliday.net/resources/customactions.html

http://blogs.msdn.com/dipper/archive/2006/10/05/How-to-Remove-or-hiding-items-in-List-toolbar-in-Sharepoint-Server-2007.aspx

http://asadewa.wordpress.com/2008/02/23/removing-single-file-upload-menu-from-document-library-toolbar-moss-2007/

Hide "View All Site Content" link for anonymous users in SharePoint WSS 3.0

This solution requires you to install Microsoft Office SharePoint Designer.
  1. Go to "Site Actions" ==> "Site Settings"
  2. Under Galleries, click on "Master Pages"
  3. Select default.master and select "Edit in Microsoft Office SharePoint Designer".
  4. In "Design" view locate "View All Site Content" on the left-hand-site navigation bar and click on it.
  5. Go to "Code" view. You will see the following code highlighted:

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ViewFormPages">
<div class="ms-quicklaunchheader">
<SharePoint:SPLinkButton id="idNavLinkViewAll" runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" Text="<%$Resources:wss,quiklnch_allcontent%>" AccessKey="<%$Resources:wss,quiklnch_allcontent_AK%>"/>
</div>
</SharePoint:SPSecurityTrimmedControl>

  1. Change the PermissionString attribute value of the Sharepoint:SPSecurityTrimmedControl XML element from ViewFormPages to BrowseDirectories.
  2. Save the default.master. If you login as an anonymous user you will not see the "View All Site Content" link. However when authenticated you will see this option.
NOTE: The "BrowseDirectories" permission is common to both the "Members" and "Owners" group permission levels, but is not set for the "Limited Access" (anonymous users) permission level. See a complete list of the SPBasePermissions Enumeration here.