Showing posts with label Tricks. Show all posts
Showing posts with label Tricks. Show all posts

Wednesday, June 10, 2009

Post build script for sharepoint based projects

Scenario:
Visual Studio projects doesn't provide any support for the WSP Deployment. And usually a developer need to perform lot of addition steps to make sure deployment is smooth. Here's a script i commonly used

Steps:
Assuming you have a single server farm
1. Make sure you have the correct WSP Builder location
2. Make sure to have a Solution folder created where this script will drop the WSP

Code:


@SET TEMPLATEDIR="c:\program files\common files\microsoft shared\web server extensions\12\Template"
@SET STSADM="c:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm"
@SET WSPPBUILDER="C:\Code\WSPBuilder\Buildx86\WSPBuilder.exe"
@SET WSPNAME="MySolution.wsp"
@SET MAINSITEURL="http://localhost"
@SET CENTRALADMINURL="http://localhost:80808"

Echo Creating Solution Package
del Solution\*.* /Q

@echo off
Echo Creating Solution Package
%WSPPBUILDER% -outputpath Solution -wspname %WSPNAME%

Echo Recycling AppPool ( To avoid any file locking issues )
IISRESET

Echo ==== Starting Clean up ===

Echo Deactivating Features
%STSADM% -o deactivatefeature -name MyFeature -url % MAINSITEURL%

Echo Uninstalling Features
%STSADM% -o uninstallfeature -name MyFeature -force

Echo Retracting Solution
%STSADM% -o retractsolution -name %WSPNAME% -immediate
%STSADM% -o execadmsvcjobs

Echo Deleting Solution
%STSADM% -o deletesolution -name %WSPNAME%

Echo Deleting all the deployed files including GAC-ed Assemblied
rmdir %TEMPLATEDIR%\FEATURES\MyFeature /S /Q

Echo ==== Done Clean up ===
Echo
Echo ==== Starting Fresh ===
Echo Adding Solution
%STSADM% -o addsolution -filename Solution\%WSPNAME%

Echo Deploying solution
%STSADM% -o deploysolution -name %WSPNAME% -immediate -allowGacDeployment
%STSADM% -o execadmsvcjobs

Echo Recycling IIS
IISRESET

Echo Activating Feature
%STSADM% -o activatefeature -name MyFeature -url % MAINSITEURL%

Echo ==== Done fresh deployment ===
Note:
This script hold good for sandbox and need changes to script for an other type deployment like Development , Staging and Production.

Tuesday, June 9, 2009

Quickly getting new GUID in text editor

Scenario:
During my sharepoint development I need to generate new Guid's all the time

Solution:
Visual Studio does provide a GuidGen.exe tool, but its kinda multi-step process.
After some research I got the solution - MACRO , my old pal :-)

Step:
1. VS > Macros > Macro Explorer

2. Create a new Module and paste the below Macro code into it


3. Go to VS > Tools > Options

4. Create a key board short cut of your choice , I used Ctrl+Alt+G as shown below


Macro Code:

Sub Create_GUID
Dim sel As TextSelection
sel = DTE.ActiveDocument.Selection
sel.Text = System.Guid.NewGuid.ToString("D").ToLower()
End Sub
Article:
Generating-guids-in-the-visual-studio-ide , Create online guid

Follow on Twitter

Saturday, June 6, 2009

Programatically adding text to clipboard

Scenario:
For one of my tiny console application , I wanted to make it easier for my end users . This console application generates some text and users need to copy it to clipboard and then paste it on to another application. Thought of making it simpler.

Solution:
I discovered its preety easy with clipboard class

Code:

using System.Window; // For this example, the data to be placed on the clipboard is a simple
// string.
string textData = "I want to put this string on the clipboard.";

// After this call, the data (string) is placed on the clipboard and tagged
// with a data format of "Text".
Clipboard.SetData(DataFormats.Text, (Object)textData);
Article:
Clipboard class
Follow on Twitter

Sunday, May 31, 2009

Quickly getting the public token key

Scenario:
In Sharepoint every assembly we deploy , need to strongly names. Deploying the assembly to GAC and getting the public token key from them can be length process.

Solution:


Visual Studio External Tools is here for rescue.

1. Open Visual Studio
2. Go to Tools > External Tools > Add a new tool with below settings

Setting 1:

Title : GetPublicTokenKey

Command : C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sn.exe

Arguments : -T $(TargetPath)

Use Output Window : Checked
OR
Setting 2 ( Need Reflector):
Title : GetPublicTokenKey

Command : C:\Reflector\reflector.exe <-- Change accordingly

Arguments : "$(BinDir)$(TargetName)$(TargetExt)" -debug

Use Output Window : Checked


Now anytime you need to read the public token key , no need to deploy the assembly to GAC .

Just go to Tools >> GetPublicTokenKey and you will get the public token key value in your output window.

Wednesday, March 25, 2009

Activating multiple features in one shot

Scenario:
You want to activate multiple features in one shot.

Solution:
I hacked this solution from out-of-box TeamCollab feature. The TeamCollab feature is an interesting feature because it uses a technique known as feature dependency. Feature dependency enables you to package several features in one feature and activate them all by activating the containing feature. Below is the example of the technique.

Code:

< ?xml version=”1.0” encoding=”utf-8”? >
< Feature Id=”NewGUID”
Title=”BundleFeature”
Description=”This is bundle feature”
ImageUrl=”FeatureLogo.gif”
ImageUrlAltText=””
Scope=”Web”
DefaultResourceFile=”core”
xmlsn=”http://schemas.microsoft.com/sharepoint/” >
< ActivationDependencies >

< !-- Feature 1 -- >
< ActivationDependency FeatureId=”Feature1_GUID” / >

< !-- Feature 2 -- >
< ActivationDependency FeatureId=”Feature2_GUID” / >

< /ActivationDependencies >
< /Feature >

Friday, January 16, 2009

File path for the GAC assemblies

Scenario:
I use Visual Studio and Reflector quite often and one common thing I usually do with both is reference binaries from GAC.
So whats the best way to get the file path to GAC assemblies.

Solution:
Command line provide a special switch for the same. Here's the example.

Code:


cd C:\windows\Assembly
dir BinaryName.dll /s

Friday, August 29, 2008

Including / Exculding a module from Site Definition based on Root level or child level web

Scenario:
As of part of definition you want to upload certain files based on weather its a Root Level Web or sub-site.

Solution:
To upload files, documents as a part of site creation , Site definition core file ONET.xml uses Module nodes, which support 'RootWebOnly'. Setting a value of TRUE for the same will result in upload of the files included in the Module only if it a Root Level Web.

Assuming that your Site Defintion name is 'ClientSiteDefinitionName' and you have a folder under that with the name 'WebParts' which contains 'MyCustom.webpart' file.

Snippet from ONET.xml:

<Module Name="CustomWebParts" List="113" Url="_catalogs/wp" SetupPath="SiteTemplates\ClientSiteDefinitionName\WebParts" RootWebOnly="TRUE">
<File Url="MyCustom.webpart" Type="GhostableInLibrary" />
</Module>

Configuring site after site creation

Scenario:
SharePoint doesn't support any event that fires before and after site creation but in many cases you want one of the following:
1. You want to execute some code after site is created.
2. You want to redirect site creator to a custom designed configuration page once site is created.

Solution:
There are various ways to do it but site definition core file ONET.xml support a special node 'ExecuteUrl' which basically takes a valid Url parameter and redirects the site creator to that Url once site is instantiated.

Snippet from ONET.xml:

<ExecuteUrl Url="_layouts/[%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%]/customsettings.aspx"/>

Specify custom JS file and Site Logo for a site definition

Scenario:
You want to include a custom JavaScript file, CSS file or a custom logo for your new site definition.

Solution:
Site Definition core file ( ONET.xml ) has top level Project node which supports 'AlternateCSS' , 'CustomJSUrl' and 'SiteLogoUrl' , so use them. make sure you package the required JS and Logo files along with the site definition.

Snippet from ONET.xml:

<Project Title="Company Site Defintion"
Revision="1" ListDir="Lists" AlternateCSS= "/_layouts/Client/custom.css"
CustomJSUrl="/_layouts/Client/custom.js"
SiteLogoUrl="/_layouts/Client/Images/ClientLogo.gif" >
Article:
http://msdn.microsoft.com/en-us/library/ms438080.aspx

Wednesday, August 6, 2008

Using relative Url ( $SPUrl )

Scenario:
You need to reference a file ( image , css or javascript file ) stored in one of the local Library using a relative Url

Option:
You can refer to the file like this "/LibraryName/filename.extension" . This works fine only when you are working with the site collection at root level. The browser doesn't know or care about site collections, and interprets the path as being relative to the website.

In this case, in a site collection at "/sites/NewSiteCollection" this path will still resolve to
http://servername/LibraryName/filename.extension
and not
http://servername/sites/NewSiteCollection/LibraryName/filename.extension

Solution:
Use $SPUrl token as shown below

<% $SPUrl:~SiteCollection/LibraryName/filename.extension %>
<% $SPUrl:~Site/LibraryName/filename.extension %>
Another important thing to note is that you can only use this with controls having runat="server" , otherwise sharepoint will not be able to resolve the $SPUrl token.

Applies to:
MOSS only ( out of box ) ,
For WSS you need to write your own Expression Builder and register it in WebConfig

:-)