Sunday, March 14, 2010

Custom Web Service in MOSS 2007

It was a bad time for me when I was trying to run custom web service in Sharepoint. I was trying and trying and getting weird situations again and again. Anyway,finally it was done and I want to share it with others to save some of their time.

Step 1. Create an ASP.NET Web Service Application in VS 2008. Rename your Service1.asmx file and class name according to your desired name both in .cs file and in markup file.

There are no step 2 and 3, go to step 4 :)

Step 4. Create a strong name for the dll of your project. In Solution Explorer, right-click the web service project, and in the Properties dialog box, click Signing, select Sign the assembly, and select in the box and create a new strong name key file. Now build the project.

Step 5. Copy the .asmx file to C:\Inetpub\wwwroot. Open the file and delete this portion -


CodeBehind="YourService.asmx.cs"

Go to GAC, right click on your assembly and copy the publick key from properties. Your final markup looks like this.

<%@ WebService Language="C#" Class="YourService.YourService, YourService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=615f0967ce2cd86a" %>

Step 6. Now you have to create .disco and .wsdl files and modify them. There are two ways to do this - creating and modifying manually or using some tool. I prefer the second way but both are explained here.

First Method -
>Run the command in cmd -

set path=%path%;"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin";.

> Now run this command in cmd -

Disco http://localhost/YourService.asmx

YourService.disco and YourService.wsdl files will be created.

>To register namespaces of the Windows SharePoint Services object model, open both the .disco and .wsdl files and replace the opening XML processing instruction -- -- with instructions such as the following:


<%@ Page Language="C#" Inherits="System.Web.UI.Page"%><%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint.Utilities" %> <%@ Import Namespace="Microsoft.SharePoint" %><% Response.ContentType = "text/xml"; %>

>In the .disco file, modify the contract reference and SOAP address tags to be like the following example, which replaces literal paths with code generated paths through use of the Microsoft.SharePoint.Utilities.SPEncode class, and which replaces the method name that is specified in the binding attribute:


<contractref ref="&lt;%" wsdl="" output="" docref="&lt;%" xmlns="http://schemas.xmlsoap.org/disco/scl/"><soap address="&lt;%" q1="http://tempuri.org" binding="q1:FilesSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/"><soap address="&lt;%" q2="http://tempuri.org" binding="q2:FilesSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/">

> In the .wsdl file, make the following, similar substitution for the SOAP address that is specified:


<soap:address location="&lt;%">

>Make the following substitution for the SOAP12 address:


<soap12:address location="&lt;%">

> Rename both files in the respective formats YourServicedisco.aspx and YourServicewsdl.aspx so that your service is discoverable through SharePoint.

Second Method -
>Go to the link http://www.codeproject.com/KB/XML/wss_web_service_helper.aspx and download the tool.

>Use the tool to create YourServicedisco.aspx and YourServicewsdl.aspx files.

Step 7. Now you copy YourServicedisco.aspx,YourServicewsdl.aspx and YourService.asmx files to _vti_bin virtual directory which maps physically to the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI directory, which contains the default Web service files used in Windows SharePoint Services.

Step 8. To make your Web service discoverable in Visual Studio as a Web service alongside the default Windows SharePoint Services Web services, open the spdisco.aspx file located in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI and add the following code, specifying the .asmx file for your Web service.


<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/YourService.asmx?wsdl"), Response.Output); %>
docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/YourService.asmx"), Response.Output); %>
xmlns=" http://schemas.xmlsoap.org/disco/scl/ " />
<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/YourService.asmx?disco"),Response.Output); %>
xmlns="http://schemas.xmlsoap.org/disco/" />

Step 9. Now go to the link http://yoursite:port_number/_vti_bin/yourservice.asmx. You will see the functions of web service listed in the page.

Step 10. Click a function to see it's Soap. You have to use this soap to call the web service method and retrieve it's returned value.

Here I am giving a jquery example of calling and displaying the data using the soap.

Soap Request

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>


Soap Response

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>

Just create a Content Editor web part and paste this code to call the Web Service.



<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function btnClicked(){

var soapEnv =
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\
<soap:Body>\
<NewMethod xmlns='http://tempuri.org/'>\
<test>"+'Test'+"</test>\
</NewMethod>\
</soap:Body>\
</soap:Envelope>";

$.ajax({
url: "_vti_bin/SPWSData.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});
}

function processResult(xData, status) {
alert(status);
$(xData.responseXML).find("NewMethodResult").each(function()
{
var data = $(this).text();
alert(data);
});
}
</script>

<input type="button" onclick="btnClicked()" value="Click"/>

Ok,this is all about creating and hosting custom web service in MOSS 2007

Thursday, March 11, 2010

Webpart containing usercontrol in MOSS 2007

Step 1. Create a web application.

Step 2. Create a UserControl in this web application.

Step 3. Add another Project (sharepoint webpart) to this solution.

Step 4. Add the following code to the .cs file of the webpart. This is the minimal code to load a usercontrol in a webpart.


Control controlObject;
String userControlURI = @"~/UserControls/YourUserControl.ascx";
public FCWebPart()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();

try
{
controlObject = this.Page.LoadControl(userControlURI);
if (controlObject != null)
{
controlObject.ID = "UCID";
Controls.Add(controlObject);
}
}
catch { }
}

protected override void Render(HtmlTextWriter writer)
{
try
{
controlObject.RenderControl(writer);
}
catch (Exception e)
{
writer.Write(e.Message);
}
}

Step 5. Create a folder named UserControls in the virtual directory (the folder of the site).

Step 6. Place the .ascx file of the usercontrol in this folder.

Step 7. Open the .ascx file in notepad and delete the followong code "CodeBehind="YourUserControl.ascx.cs"

Step 8. Build the solution and copy the dll of the web application.

Step 9. Put the dll in the bin directory of virtual directory.

Step 10. Right click on the webpart project and see the properties. Here put the link of the sharepoint site in Debug box.

Step 11. Now Right click on the webpart project and click Deploy.

Wednesday, March 10, 2010

Custom Webpart in MOSS 2007

Webparts are Cool, Hot and Flexible. Ok,this was a joke, but webparts are really very helpful to add some customized support to your sharepoint site. I hope you have the basic understanding of what is webpart and how to add/remove a webpart from sharepoint site. Here I will discuss two ways to deploy custom sharepoint site, the first one is messy and the second one is hilarious(not so sure about the adjective!). Read the first one if you want to curse me or skip to the next one.

Prerequisite :
Install an extension for your VS, you will find it in http://www.microsoft.com/downloads/details.aspx?FamilyID=B2C0B628-5CAB-48C1-8CAE-C34C1CCBDC0A&displaylang=en or google with "visual studio extension for sharepoint 1.3" if fail to click/copy the link from here(is not that an impossible case?) This extension is known as VSEWSS.

First Method :

Step 1 : Open VS2008 and try to create a new Project. As you have installed VSEWSS you will find a new category in create project named “SharePoint”. Choose an Empty template.

Step 2 : Now in your blank project add a Web Part through Add -> New Item -> Web Part. A new web part is created in your project. Write your code here according to your need or leave it as it is if you are only learning.

Step 3 : Now right clicking on the project in Solution Explorer you will find a new option, “Package”. Click it and your web part is done :)

Now the next is to deploy it...

Step 4 : Now add "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN" this to your path variable. Why? I will not tell you.Google for the answer if you don’t know. To make it path variable run this in cmd,

>set path=%path%;"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN";.

Step 5 : Now in cmd go to Debug folder of your project. You will find a .wsp file in this folder named after your webpart. Now to add your WebPart to your Sahrepoint site run the following command.

>stsadm -o addsolution -filename FirstWebPart.wsp

Step 6 : Now open Sharepoint Central Administration. Go to Central Administration > Operations > Solution Management

If you are not damn unlucky you will find your web part here listed. Click it and Deploy it.

Step 7 : Now go to your sharepoint site. Click Site Actions and go to Site Settings > Site Features

Here you will find your web part,just activate it.

Step 8 : Now go to Site Actions>Edit Page and add you web part in the page.

Wow it’s visible in your page and you are really done with your web part :)

Step 9 : If you like to upgrade your webpart run the command in cmd in Debug folder of your project
>stsadm -o upgradesolution -name FirstWebPart.wsp -filename FirstWebPart.wsp -immediate –allowgacdeployment

Or
Just paste the dll of the solution into GAC and iireset. For this you have to assign strong name to your dll,Google for how to assign strong name to dll.

Second Method :
This is same up to Step 3 of First Method, just the deployment portion is different. So follow up to Step 3 and then

Step 4 :
Build the webpart solution and copy the dll of the web application.

Step 5 : Put the dll in the bin direstory of virtual directory of your site.

Step 6 : Right click on the webpart project and see the properties. Here put the link of the sharepoint site in Debug box and put a new key file name Signing box. Build the project.

Step 7 : Now Right click on the webpart project and click Deploy.

Step 8 : Now go to your sharepoint site. Click Site Actions and go to Site Settings > Site Features

Here you will find your web part,just activate it.

Step 9 : Now go to Site Actions>Edit Page and add you web part in the page.

Wow it’s visible in your page and you are really done with your web part :)

At last found a great way to paste code in Blogger

If you ever have tried to use code in your blogger site you surly have come to know about the syntaxhighlighter project. It's a great tool with lots of features. But I was looking for some more flexible ways to work with and at last found a very handy tool. There is an add-on for almost everything in firefox and so is for this purpose. The Quick paste code to Blogger is really a nice add-on. Link to this add-on is https://addons.mozilla.org/en-US/firefox/addon/11911 Just install it and you will get a button named Code in your blogger New Post page. Just click it and paste your code and select the language :)

Pretty cool, huh?

Friday, January 8, 2010

Creating Custom Template in Share Point2007(MOSS 2007)

Follow the steps to create and add a custom template in Sharepoint 2007.

1. Go to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates and make a copy of any of the folders here. Each folder here means a Template.

2. Rename the copied folder with your desired template name,like TEST. Keep in mind that the name should be in uppercase.

3. Go to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\XML folder and make a copy of any of the Webtemp*.xml file.

4. Rename the copied file to WebtempTEST.xml.

5. Open the WebtempTEST.xml and replace all attributes with TEST. Set DisplayCategory="Custom".

6. Restart IIS.

7. Now create a site collection and choose the created Template there.

Creating Custom Masterpage in Share Point2007(MOSS 2007)

I found two ways to create and add a custom masterpage in Share Point 2007(MOSS 2007). They are described below.

First procedure :

1. First go to the folder where masterpages are stored. The path is Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\PublishingLayouts\MasterPages

2. Now you can create a new masterpage using Sharepoint Designer or make a copy of an existing masterpage. Give it a new name like Test.master

3. Go to the following folder Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\PublishingLayouts and open the file named ProvisionedFiles.xml

4. Find the OSGMasterPages Module tag and make a copy of any File tag block within this Module tag block. Here is a File tag block for every master page. Suppose you copied BlackSingleLevel.master block

5. In your copied block replace all BlackSingleLevel with Test.

6. Update the description of your master page in MasterPageDescription block.

7. In the next Module, PublishingLayoutsPreviewImages, create an entry for the preview image by copying and pasting and existing FILE tag and updating the properties,I mean the .png file name.


8. Now restart IIS.

9. Now go to your sharepoint site and go to here Site Actions -> Site Settings -> Site collection features and activate Office SharePoint Server Publishing Infrastructure if not activated.

10. Now go to Site Actions -> Site Settings -> Master Page(under Look and Feel) and you will find two dropdown lists of masterpages. Select yours(Test.master) from here.

11. You are done :)

Second Procedure :

1. Create a Masterpage using Sharepoint Designer or by coping an existing one.

2. Now go to your sharepoint site and go to here Site Actions -> Site Settings -> Site collection features and activate Office SharePoint Server Publishing Infrastructure if not activated.

3. Now go to Site Actions -> Site Settings -> Master Page(under Galleries) and upload your masterpage.

4. Now go to Site Actions -> Site Settings -> Master Page(under Look and Feel) and you will find two dropdown lists of masterpages. Select yours from here.

Second one is easy,hah?

Now remember few things when making your own masterpage. There are some things which must be contained in your masterpage. Go to http://msdn.microsoft.com/en-us/library/aa660698.aspx and check the minimal masterpage. Keep in mind that you have to add all the contentplaceholder of this minimal masterpage in your masterpage.

Creating Custom Masterpage in Share Point2007(MOSS 2007)

I found two ways to create and add a custom masterpage in Share Point 2007(MOSS 2007). They are described below.

First procedure :

1. First go to the folder where masterpages are stored. The path is Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\PublishingLayouts\MasterPages

2. Now you can create a new masterpage using Sharepoint Designer or make a copy of an existing masterpage. Give it a new name like Test.master

3. Go to the following folder
Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\PublishingLayouts and open the file named ProvisionedFiles.xml

4. Find the OSGMasterPages Module tag and make a copy of any File tag block within this Module tag block. Here is a File tag block for every master page. Suppose you copied BlackSingleLevel.master block

5. In your copied block replace all BlackSingleLevel with Test.

6. Update the description of your master page in "This is Test.master" />

7. In the next Module, PublishingLayoutsPreviewImages, create an entry for the preview image by copying and pasting and existing FILE tag and updating the properties.
" Type="GhostableInLibrary">


8. Now restart IIS.

9. Now go to your sharepoint site and go to here Site Actions -> Site Settings
-> Site collection features and activate Office SharePoint Server Publishing Infrastructure if not activated.

10. Now go to Site Actions -> Site Settings -> Master Page(under Look and Feel) and you will find two dropdown lists of masterpages. Select yours(Test.master) from here.

11. You are done :)

Second Procedure :

1. Create a Masterpage using Sharepoint Designer or by coping an existing one.

2.
Now go to your sharepoint site and go to here Site Actions -> Site Settings -> Site collection features and activate Office SharePoint Server Publishing Infrastructure if not activated.

3. Now go to
Site Actions -> Site Settings -> Master Page(under Galleries) and upload your masterpage.

4.
Now go to Site Actions -> Site Settings -> Master Page(under Look and Feel) and you will find two dropdown lists of masterpages. Select yours from here.

Second one is easy,hah?

Now remember few things when making your own masterpage. There are some things which must be contained in your masterpage. Go to http://msdn.microsoft.com/en-us/library/aa660698.aspx and check the minimal masterpage. Keep in mind that you have to add all the contentplaceholder of this minimal masterpage in your masterpage.