Saturday 5 July 2014

SharePoint create the WSP Package

Steps to create the WSP Package
1. Create the solution with WSPBuilder project(Open VS--> file NewProject WSPBuilder)
2. Add the “TEMPLATE” folder in the project
3. Add the “FEATURES” folder under “TEMPLATE” folder
4. Add the one sample folder (ex: SampleAuditing) under “TEMPLATE” folder
5. Add aspx pages, elements.xml, feature.xml files in to this folder
6. Add the “Microsoft.SharePoint.dll” and “Microsoft.SharePoint.portal.dll” as references.
7. add a folder “Ex: SampleFeature” for feature to activate/deactivate the feature under project
Folder structure
8. add the feature activated class in the “SampleFeature “ folder
Feature activated and deactivated code
public class AuditFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = SPContext.Current.Site;
if (site != null)
{
QuickLanchMenu.AddLinkToQuickLaunch("NAS Auditing", "/SampleAuditing/Audit.aspx");
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
QuickLanchMenu.RemoveLinkFromQuickLaunch("NAS Auditing");
}
}
}
9. Add QuickLanchMenu class in the project and add the following code.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
namespace SampleWSP
{
public class QuickLanchMenu
{
///
/// Add Link From QuickLaunch Menu
///
///
///
public static void AddLinkToQuickLaunch(string strTitle, string strURL)
{
SPSite site = SPContext.Current.Site;
using (SPWeb quickLaunchWeb = site.OpenWeb())
{
if (site != null)
{
SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;
SPNavigationNode abbottMenuItem = new SPNavigationNode(strTitle, quickLaunchWeb.Url + strURL, true);
quickLaunchNodes.AddAsLast(abbottMenuItem);
int menuItemID = abbottMenuItem.Id;
if (!quickLaunchWeb.Properties.ContainsKey(strTitle))
{
quickLaunchWeb.Properties.Add(strTitle, menuItemID.ToString());
}
else
{
quickLaunchWeb.Properties[strTitle] = menuItemID.ToString();
}
quickLaunchWeb.Properties.Update();
quickLaunchWeb.Update();
}
}
}
///
/// Remove Link From QuickLaunch Menu
///
///
public static void RemoveLinkFromQuickLaunch(string strTitle)
{
SPSite site = SPContext.Current.Site;
if (site != null)
{
int menuItemID;
using (SPWeb quickLaunchWeb = site.OpenWeb())
{
SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;
if (int.TryParse(quickLaunchWeb.Properties[strTitle].ToString(), out menuItemID))
{
SPNavigationNode abbottMenuItem = quickLaunchNodes.Navigation.GetNodeById(menuItemID);
if (abbottMenuItem != null)
{
quickLaunchNodes.Delete(abbottMenuItem);
}
quickLaunchWeb.Properties[strTitle] = null;
quickLaunchWeb.Properties.Update();
quickLaunchWeb.Update();
}
}
}
}
}
}
10. In the feature.xml file assign the feature activated class namespace and class name to the ReceiverClass property
Add the following lines of code in Feature.xml
PublickeyToken value is publickeytoken of the project dll
ReceiverClass value is {NameSpace.ClassName } of the feature class
11. In the element .xml file give the module and aspx page URLs.
Add the following lines of code in element.xml
ModuleURL is FolderName of the module(ex: SampleAuditing)
File URL is Page name with in the module
UrlAction URL is complete path of the page in the web site
12. Add the following lines of code in Audit.aspx page
PublickeyTokenKey value is publickeyTokenKey of the current project library.
13. Build the solution
14. Build the project with “WSPBuilder” option.
15. Find the ProjectName.wsp file in project location
16. Deploy wsp file with following instructions
cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
stsadm -o retractsolution -name "SampleWSP.wsp" -immediate -allcontenturls
stsadm -o execadmsvcjobs
stsadm -o deletesolution -name "SampleWSP.wsp" -override
stsadm -o execadmsvcjobs
stsadm -o addsolution -filename "D:\Samples\SampleWSP\SampleWSP\SampleWSP.wsp"
pause