Tuesday, May 18, 2010

Retrieving Public Key Token

At times we need to know the Public Key Token for a strongly named assembly (dll) in .Net. If somebody is working with SharePoint then it becomes a daily affair.

There is a good tool available called ".Net Reflector" (http://www.red-gate.com/products/reflector/) to easily get the Public Key Token. However, we can also use the .Net Framework tools sn.exe. The sn.exe would only work if the assembly has been strongly signed.

So open the Visual Studio 2008 Command Prompt and then point to the dll’s folder you want to get the public key. Use the following command.

sn –T myDLL.dll


Example


C:\WINNT\Microsoft.NET\Framework\v3.5>sn -T ABCD.exe
Microsoft (R) .NET Framework Strong Name Utility Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.
Public key token is d1850ec307b5340d

Note: We can also use the assembly folder(GAC) which is located at - c:\windows\assembly (in Run – write assembly) right click on the desired assembly(dll), on the assembly property we can find the public key. Further to get a dll from GAC - C:\windows\assembly\gac_msil

It is very easy to accomplish the task... isn't it... HaPpY CoDinG

Partha (Aurum)

Creating StiePages with Code Behind.

StiePages with Code Behind(Feature,install.bat)

The aspx pages : Need to convert the third brackets to angular brackets

[%@ Page Language="C#" MasterPageFile="~masterurl/default.master" Debug="true" Inherits="TestSitePage,SitePages,Version=1.0.0.0,Culture=neutral,PublicKeyToken=5241345d6ad43482" meta:progid="SharePoint.WebPartPages.Document" %]

[asp:content id="C1" runat="server" contentplaceholderid="PlaceHolderPageTitle"]

Test Site Page [/asp:content]

[asp:content id="C2" runat="server" contentplaceholderid="PlaceHolderMain"]

[asp:label id="Label1" runat="server" text="Label"][/asp:label]

[asp:button id="Button1" runat="server" text="Button"]

[/asp:button][/asp:content]

[asp:button id="Button1" runat="server" text="Button"] [/asp:button]

Now in the C# page...

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.WebControls;

using System.Web.UI.WebControls;

using System.Web.UI;

public partial class SitePages: Page

{

protected Label Label1;

protected Button Button1;

//protected override void OnPreInit(EventArgs e)

//{

// base.OnPreInit(e);

// //this.MasterPageFile = SPContext.Current.Web.ServerRelativeUrl + "/_catalogs/masterpage/default.master";

// this.MasterPageFile = SPContext.Current.Web.MasterUrl;

//}

protected override void OnInit(EventArgs e)

{

//base.OnInit(e);

Button1.Click += new EventHandler(Button1_Click);

}

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = Label1.Text.Trim() + DateTime.Now.ToString();

}

//protected void Page_Load(object sender, EventArgs e)

//{ Label1.Text = "Hello World Code Behind";

//}

}

-----

Feature.xml

[feature id="5A8158F4-2AAD-4608-A03F-F33A7FDA3945" title="LA Site Pages" description="These are the LA Site Pages" xmlns="http://schemas.microsoft.com/sharepoint/" scope="Web" version="1.0.0.0" hidden="FALSE"]

[elementmanifests]

[elementmanifest location="Elements.xml"]

[elementfile location="TestPage1.aspx"]

[elementfile location="StartPage.aspx"]

[/elementfile][/elementfile][/elementmanifest][/elementmanifests]

[/feature]

---

Elements.xml

[elements xmlns="http://schemas.microsoft.com/sharepoint/"]

[module url="Sample"]

[file url="TestPage1.aspx" name="TestPage1.aspx" type="Ghostable"]

[/file]

[/module]

[module url=" Sample"]

[file url="StartPage.aspx" name="StartPage.aspx" type="Ghostable"]

[/file]

[/module]

[customaction id="757CEE41-D25E-47f3-A088-A9ED11C896F6" title="LA Test Pages" description="Shows the SP Site Test Pages" location="Microsoft.SharePoint.StandardMenu" groupid="SiteActions" sequence="99"]

[urlaction url="~site/Sample/TestPage1.aspx"]

[/urlaction][/customaction]

[customaction id="669B5931-0A2C-4446-A73E-0FBEAD549833" title="LA Start Pages" description="Shows the SP Site Start Pages" location="Microsoft.SharePoint.StandardMenu" groupid="SiteActions" sequence="99"]

[urlaction url="~site/Sample/StartPage.aspx"]

[/urlaction][/customaction]

[/elements]

--------

Post Build

cd $(ProjectDir)

Install.bat

--------

Install.Bat

REM – Remember to remove line breaks from first two lines

@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 GACUTIL="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\GacUtil.exe"

Echo Copying files

xcopy /e /y TEMPLATE\* %TEMPLATEDIR%

Echo Installing feature

%STSADM% -o InstallFeature -filename Pages\feature.xml -force

Echo Uninstalling the DLL from GAC

%GACUTIL% /u SitePages

Echo Installing the DLL in GAC

Echo %GACUTIL%

%GACUTIL% /i "C:\Partha\Projects\SitePages\bin\Debug\ SitePages.dll"

Echo Restart IIS Worker Process IISRESET



HaPpY CoDinG

Partha (Aurum)


Monday, May 17, 2010

How to set custom master page as default master page with site definition in Moss and WSS 3.0?

How to set custom master page as default master page with site definition in Moss and WSS 3.0?

Changing a default master page for our custom need is not at all good idea and creating a custom master page and use it as default master page is a headache. Here is the way to create and use your custom master page with site definition.

Go to “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL” where you can find default master page (default.master).

Copy that default.master and pest it with customdefault.master or your own custom name.
(Note: while copying default.master please take care that you must not have already modified It. Use a fresh copy)
Put your custom master page at same location as default.master.
Now go to “12\TEMPLATE\GLOBAL\XML” open “ONET.xml”

At the last you will find module tag which provision “default.master” same way add your entry for custom.master.

[Modules]
[Module Name="DefaultMasterPage" List="116" Url="_catalogs/masterpage" RootWebOnly="FALSE"]
[File Url=" customdefault.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" /]
[File Url="default.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" /]
[/Module]
[/Modules]

This code will automatically put your custom masterpage into the masterpage gallery when a new site is created.

Now open your onet.xml file from 12/template/SiteTemplates
Change the default masterpage to the new one.
Find [Configuration] tag
Add MasterUrl attribute like MasterUrl=”_catalogs/masterpage/customdefault.master” and if you already have this tag change it with your custom master page.
So it will look like

[Configuration ID="0" Name="YOUR NAME" MasterUrl="_catalogs/masterpage/customdefault.master"]

Now the new site created with this definition have this master page as default master page. Click here for

What about already existing site?

For that you need to put master page manually and change master page url for each wab
Here is the code snippet (only for setting master page url of the web):
Note: use this snippet carefully. If you run this snippet and you do not have Master page in Master page gallery for that web you will get master or page not found error.

SPSite spsite = new SPSite("Your site name");
foreach (SPWeb Myweb in spsite.AllWebs)
{
string masterurl = Myweb.ServerRelativeUrl;
if (!masterurl.EndsWith(@"/"))
masterurl = masterurl + @"/";
Myweb.MasterUrl = masterurl + "_catalogs/masterpage/customdefault.master";
Myweb.Update();
}