Tuesday, September 14, 2010

WSS V 3.0 provides option for us to extend the stsadm commands and customize the operations as per our requirements .
I have extended stsadm to one such command that list outs the list and libraries for a specified sharepoint site .

Namespaces used : Microsoft .Sharepoint and Microsoft.Sharepoint.StsAdmin

The process involves 4 steps as mentioned below :
1.Create a class library project with custom class extending the ISPStadmCommand Interface . Enclosed is the code and comments are inline
namespace ExtendStsadm
{
public class CustomStsadm :ISPStsadmCommand
{
//Get help on the extended command
//execute stsadm -o enumlist to get help

public string GetHelpMessage(string command)
{
return "-url ";
}

//Run() executes the command extended
public int Run(string command, StringDictionary keyValues, out string output)
{
command = command.ToLower();
switch (command)
{
case "enumlist": return this.EnumLists(keyValues,out output);
default: throw new InvalidOperationException();
}
}

public int EnumLists(StringDictionary keyValues ,out string output)
{
if (!keyValues.ContainsKey("url"))
{ throw new InvalidOperationException("The url parameter was not specified.");
}
String url = keyValues["url"];
SPSite oSite = null;
SPWeb oWeb = null;
StringBuilder oStringBuilder = new StringBuilder();
//loop through the site to list out the List /libraries for it
try
{
oSite = new SPSite(url);
oWeb = oSite.OpenWeb();
SPListCollection oListColl=oWeb.Lists;
foreach (SPList oList in oListColl)
{
oStringBuilder.AppendLine(oList.Title.ToString());
}
output = oStringBuilder.ToString();
}
catch (Exception ex)
{ throw new InvalidOperationException(ex.Message.ToString());
}
return 0;
} }}

2.String name the assembly ,build the project ,move it to GAC and do an iisreset .
3.Now ,we need to add a configuration entry for registering class and assembly defined.
Go to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG ,Create an xml
file to hold this configuration and name it as stsadmcommands.[ClassName].xml and add the config entry
enclosed and save the file:
4.Execute the command
stsadm -o enumlist -url [url of the sharepoint site]
Now , we should be able to enumerate the list/libraries for a specified sharepoint site.

Tuesday, August 31, 2010

Impersonation in Sharepoint 2007

Found out a good article , that answers when and how to impersonate in sharepoint 2007 .

http://vspug.com/tanujashares/2007/08/07/impersonation-in-sharepoint-2007/

Thursday, August 12, 2010

Customise the navigation in Sharepoint

We can customise the navigaiton in a sharepoint site using the WSS V 3.O navigation store , that is represented by the namespace Microsoft.Sharepoint.Navigation .
It provides classes ,properties and methods to customise the navigation of a sharepoint site .
lets see how to add a custom link to a quick launch bar /top navigation bar on activating a feature
class CustomLinkQuickLaunch : SPFeatureReceiver
{

//Get the context of the current site
SPSite oSite = SPContext.Current.Site;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

using(SPWeb oWeb = oSite.OpenWeb())

{
//Get the tree view colllection of nodes in the quick launch bar
SPNavigationNodeCollection nodeColl=oWeb.Navigation.QuickLaunch;

//Get the tree view colllection of nodes in the top navigation bar
SPNavigationNodeCollection nodeColl=
oWeb.Navigation.TopNavigationBar;
//Add a new node to the collection giving it a titel and url , boolean value indicates whether the link is outside the current site coll or not .Since ,it is outside the current site coll we pass boolean true here
SPNavigationNode oCentralAdminNode =

new SPNavigationNode("Central Admin Home ", "url of the central admin",true);
nodeColl.AddAsLast(oCentralAdminNode);

//Now we can add a child node to the central admin link in a simialr fashion
SPNavigationNode oExtNode = new SPNavigationNode("Yahoo Home ", "http://www.yahoo.com/", true);
oCentralAdminNode.Children.AddAsFirst(oExtNode);
oWeb.AllowUnsafeUpdates = true;
oWeb.Update();

}
}
We need to delete the node from the quick launch bar/top navigation bar on feature deactivation , it will create a duplicates entry of the nodes when activated for the next time otherwise .The following code does exactly this

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using(SPWeb oWeb = oSite.OpenWeb())

{
//Get the node collection of the quick launch or top navigation bar
SPNavigationNodeCollection nodeColl=oWeb.Navigation.QuickLaunch;
SPNavigationNodeCollection oNodeColl = oWeb.Navigation.TopNavigationBar;
oNodeColl.Delete(indexing to the node collection can be done to get a node to be deleted)

oWeb.AllowUnsafeUpdates = true;
oWeb.Update();

}
}
}

Difference between Custom templates and Definition in Sharepoint

Custom templates here refers to Site/List templates while definition refers to Site/List definitions .Let me point out the key differences that helps us in our design decision ,

Templates -
Custom templates are created from the existing site /list through UI, that may or may not have contents .
Templates exists in the database .
Templates cannot be changed for further use and has to be used the way it is.
They are typically easy to create and deploy .

Definitions -
We create custom definitions by declaring the lists, fields, web features, site features ,content types ,navigation bars to be added with a site/list and registering them ,so that they can be used readily.
They exists in the web server (file system) ,hence better performance .
They can be customised and modified as & when required .
Not easy to create and deploy .
One has to have access to the Web server for deploying the definitons.

Thursday, July 8, 2010

Difference between feature dependency and feature staple in sharepoint

Many a times i have heard a lot of discussions on feature dependency and stapling in sharepoint and their application in sharepoint . Both these terminologies depends on other feature for its applications . I would like to point out my understanding on them in this section .


Feature Dependency - As per my understanding "Activation of a feature depends on the other " . To make it clear let me give a scenario where "Office SharePoint Server Publishing" feature can be activated only if "Office SharePoint Server Publishing Infrastructure" feature is activated, without which the former is not possible .
In this scenario "Office SharePoint Server Publishing" is dependent on "Office SharePoint Server Publishing Infrastructure" .

Feature Stapling - Whenever we need to activate a feature on site creation using site definitions we need to go for stapling . This is because sharepoint doesn't have synchronous/asynchronous events to capture site/web creation.For eg : lets say we need to change master page for a site on its creation then we can have a feature that does this and associate it with another feature using FeatureSiteTemplateAssociation .
Feature that has FeatureSiteTemplateAssociation is called as stapler while the one that changes master page( in this scenario) is called as staplee. This concept is used especially in branding a sharepoint site .