Tuesday, June 29, 2010

A Simple Event handler

A Simple Event handler

Creating a simple Event handler – inheriting SPEventReceivers such as SPItemEventReceiver, SPListEventReceiver


 


Feature File

<?xml
version="1.0"
encoding="utf-8"?>

<Feature
Scope="Web"

Title="Simple Event Handler Registration"

Id="69322A1C-0618-43eb-813B-82F02CBA9634"

xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest
Location="Elements.xml"/>

</ElementManifests>

</Feature>

-------------------
Elements File
<?xml
version="1.0"
encoding="utf-8"?>

<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">

<Receivers
ListTemplateId="104">

<Receiver>

<Name>MyEventHandlers</Name>

<Type>ItemDeleting</Type>

<SequenceNumber>10000</SequenceNumber>

<Assembly>SDKSPEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=89d0c098ec698224</Assembly>

<Class>SDKSPEvents.SimpleEventHandler</Class>

<Data></Data>

<Filter></Filter>

</Receiver>

</Receivers>

</Elements>

-----------

Now the class file implementing the SPItemEventReceiver for item level event handling. All the levels have their respective EventReceiver (ex. SPListEventReceiver)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

namespace SDKSPEvents

{


public
class
SimpleEventHandler : SPItemEventReceiver

{


 


public
override
void ItemDeleting(SPItemEventProperties properties)

{


//base.ItemDeleting(properties);

properties.Cancel = true;

properties.ErrorMessage = "Deleting not Permitted";

}

}


 


 


public
class
sim : SPListEventReceiver

{


public
override
void FieldDeleting(SPListEventProperties properties)

{

properties.Cancel = true;

properties.ErrorMessage = "Deleting not Permitted";

}

}

}

................HaPpY CoDinG


 

Partha (Aurum)

Thursday, June 24, 2010

STSADM RESTORE – Exception from HRESULT: 0x80040E14


 

Exception from HRESULT: 0x80040E14

I was trying to restore my site in a different box with the stsadm restore command :

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>

stsadm.exe -o restore -url http://Server:7777 -filename "D:\Partha\Backup\MyAppCom.dat" –overwrite


 

This resulted to an exception Exception from HRESULT: 0x80040E14

While trying to dig the problem, I found that my SQL server has run out of space for either the logs or the data files. Actually my D: drive ran out of space…. L. The solution was very simple deleted some not required files and copied few to other drives to make some space for SQL Server, ran the command and the restore was successful.


 

………………..HaPpY CoDiNg

Partha (Aurum)

Monday, June 21, 2010

Install.bat for VS 2008

Install.bat

A ready to use install.bat. The Xcopy part has been fully defined since it was not working properly in VS2008.


 

@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 SDKs\Windows\v6.0A\bin\GacUtil.exe"


 

Echo Copying files to TEMPLATE directory

xcopy "C:\Partha\Devarea\MyFeatureReceiver\MyFeatureReceiver1\MyFeatureReceiver1\TEMPLATE\*" "c:\program files\common files\microsoft shared\web server extensions\12\Template" /e /y


 

REM Echo Installing feature

REM %STSADM% -o InstallFeature -filename MyFeatureReceivers\feature.xml -force


 

REM Echo Uninstalling the DLL from GAC

REM %GACUTIL% /u MyFeatureReceiver1


 

Echo Installing the DLL in GAC

Echo %GACUTIL%

%GACUTIL% /i "C:\Partha\Devarea\MyFeatureReceiver\MyFeatureReceiver1\MyFeatureReceiver1\bin\Debug\MyFeatureReceivers.dll"


 

REM Echo Restart IIS Worker Process IISRESET

REM IISRESET


 


 

http://blah.winsmarts.com/2008-7-Authoring_SharePoint_2007_Workflows_using_VS2008.aspx

http://msdn.microsoft.com/en-us/library/bb466224(office.12).aspx

Deployment - Xcopy

Deployment has always been a pain area for SharePoint developers. However the good news is in SharePoint 2010 the deployment process has eased off a lot. Although the use of WSP
Builder also helps a lot for creating wsp solution packages for deployment. But during the development phase it still remains a cumbersome job for copying different file in different folder such as the putting the assemblies in the GAC or a BIN folder. I reduce this redundant process I prefer to use the install.bat file where I keep all my post build commands (incase the solution is big) otherwise, I keep the commands in the post build. Let us see the below example – here, I am putting the dlls in a separate folder Deployment\bin and other pages such as aspx, ascx in Deployment\Pages and Deployment\UserControls respectively.

The Deployment folder is in C drive:-

C:\Work\Projects\Deployment\bin

xcopy /y "$(TargetDir)*.dll" "C:\Projects\Deployment\bin\*.dll"

xcopy /y "$(projectdir)*.aspx" "C:\Projects\Deployment\Pages\*.aspx"

xcopy /y "$(projectdir)*.ascx" "C:\Projects\Deployment\UserControls\*.ascx"

On successful build of the project, this script will copy the required dlls and file at designated folder… Hope this information might help other… J

More information on XCopy:

http://commandwindows.com/xcopy.htm, http://www.15seconds.com/Issue/030806.htm, http://support.microsoft.com/kb/326355

……. HaPpY CoDinG

Partha (Aurum)


 

Wednesday, June 16, 2010

Screen Scraping

Screen Scraping (web response) with web request object


 

If you need to intercept the response stream from any url which in other words said as Screen Scraping. In the below example I have created a uri object to create a Webrequest.Create(strUrl).

Uri url = new
Uri("http://www.google.com");

WebRequest wr = WebRequest.Create(url);

WebResponse response = wr.GetResponse();

StreamReader sr = new
StreamReader (response.GetResponseStream ());

string result = sr.ReadToEnd();

Response.Write(result);


 

Note: Before accessing the url try with TelNet and check whether the url is sending any response.


 

……. HaPpY CoDinG

Partha (Aurum)

Tuesday, June 15, 2010

Getting Assemblies from GAC

Assemblies from GAC

At times we face situations wherein we need the dlls or assemblies directly. Generally the assemblies reside in the Assembly Folder which is best known as GAC (Global Assembly Cache). To open the GAC – go to run and write "assembly" (without quotes), this will open a folder at location "C:\WINDOWS\assembly". This is where we can find all the assemblies listed. Now if we would like to get the dll or assembly rather than the reference, then what..? L I tried to copy the dll.. upps.!! It will not work….

Finally found a way out to get upto the dll --- J

Steps –

  1. Go to run and write "c:\WINDOWS\assembly\gac_msil"
  2. This will open a folder where we can find the assembly folders.

For simplicity let us take and example to find the mostly used dll for SharePoint – Microsoft.SharePoint.dll . We would use the same command from the run, we could find a folder named Microsoft.SharePoint. This is the folder that contains the dll. @ C:\WINDOWS\assembly\gac_msil\Microsoft.SharePoint. On clicking the folder we would land on the folder named 12.0.0.0__71e9bce111e9429c, this is folder name shows the version number+_+ public key token ( we can get the public key token from this place as well, however this is not recommended) and inside this folder (C:\WINDOWS\assembly\gac_msil\Microsoft.SharePoint\12.0.0.0__71e9bce111e9429c)we get the desired dll - Microsoft.SharePoint.dll


 

Hope this information helps other…..

……. HaPpY CoDinG

Partha (Aurum)

GACUtil missing in Windows Server 2008/Visual Studio 2008

GACUtil missing in Windows Server 2008/Visual Studio 2008

I was developing a webpart in Visual Studio 2008 on Windows Server 2008 to be deployed in a SharePoint Site. While writing the install.bat, I could not configure my post build for my class library in Visual Studio 2008 to be deployed in GAC. The Gacutil was not found at the location – "" I wrote the Gacutil path as I used to do for Visual Studia 2005.

@SET GACUTIL="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\GacUtil.exe"

And in the Post Build Option

cd $(ProjectDir)
Install.bat

Thus, I had to look for Gacuitl.exe's path. I searched into directories below and I was surprised that it was no where to be found.....

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\GacUtil.exe

C:\Program Files\Microsoft Visual Studio 9.0\SDK\v3.5\Bin // NOT FOUND

C:\Program Files\Microsoft Visual Studio 9.0 // NOT FOUND

C:\Program Files\Microsoft.NET // NOT FOUND

Then I searched into C:\Windows\Microsoft.NET\Framework and found out gacutil.exe.config which was based on .NET 1.1. That was quite old. There was no gacutil.exe in there. However, I was able to run gacuitl from VS2008 command prompt.

Where could the file be? L

Lastly, I found out gacutil was located in -- J

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

This folder contains other assemblies like sn.exe.

The changed Install.bat

REM – Deploying WebPart


 

@SET GACUTIL="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\GacUtil.exe"


 

Echo Uninstalling the DLL from GAC

%GACUTIL% /u SitePages


 

Echo Installing the DLL in GAC

%GACUTIL% /i "C:\Partha\Devarea\WebPart\bin\Debug\WebPart.dll"


 

Echo Restart IIS Worker Process

Rem IISRESET

Hope this could be handy for others J


 

……. HaPpY CoDinG

Partha (Aurum)


 

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();
}