Customizing Custom Web Part Menu in SharePoint


Any standard web part within SharePoint would have menu options like Minimize, Close, and Modify Shared Web Part. We can even add our own custom menu options over there. They are referred to as verbs in web part’s context. We could add three types of web part menu verbs.

Client Side –We could specify JavaScript over here.

Server Side – We can attach event handler over here.

Both – We could have both JavaScript and event handler specified.

Let’s take a simple example where we would be adding all the above three kind of verbs.

Create a web part project within Visual Studio.

Put the following code for web part,

It would have a text box that would show the text set using the event handler specified in the server side verb.

[Guid(“172bc4c1-1c5d-49cd-93bc-1874acbbb9c8”)]

    public class WebPartMenuVerbWebPart : System.Web.UI.WebControls.WebParts.WebPart

    {

        public WebPartMenuVerbWebPart()

        {

        }

 

        protected  TextBox txtInfo;

        protected override void CreateChildControls()

        {           

            txtInfo = new TextBox();

            this.Controls.Add(txtInfo);           

        }

        public override WebPartVerbCollection Verbs

        {

            get

            {

                // Client side verb

                WebPartVerb clientSideVerb = new WebPartVerb(“clientID”, “javascript:alert(‘Hello World from Java Script Verb!’);”);

                clientSideVerb.Text = “Client Side Verb”;

                // Server side verb

                WebPartVerb serverSideVerb = new WebPartVerb(“serverID”, new WebPartEventHandler(ServerVerbEventHandler));

                serverSideVerb.Text = “Server Side Verb”;

                // Verb for both client side and server side

                WebPartVerb bothSideVerb = new WebPartVerb(“bothID”, new WebPartEventHandler(ServerVerbEventHandler), “javascript:alert(‘Hello World from Java Script Verb!’);”);

                bothSideVerb.Text = “Both Side Verb”;          

                WebPartVerbCollection wbVerbCollection = new WebPartVerbCollection(base.Verbs, new WebPartVerb[] { clientSideVerb, serverSideVerb, bothSideVerb  });

                return wbVerbCollection;

            }

        }

        protected void ServerVerbEventHandler(object sender, WebPartEventArgs args)

        {

            txtInfo.Text=“Hello world from Server Side Verb”;                           

        }       

    }

 

That’s it …

Fixing SQL Server Error : The backup set holds a backup of database other than the existing database.


We faced this error while restoring a database in Sql Server using the .bak(backup)  file but into a different database.

The way it was resolved for us was to

1) Selecting Options within Restore Database dialog box.

2) Specifying correct path to the mdf and log file for the database.

3) Checking Overwrite the existing database option.

That’ s it ..

Understanding Garbage Collection in Microsoft.NET


Garbage collection in Microsoft.NET is based on Mark and Compact algorithm and concept of generations.

 

Mark and Compact Algorithm

 

When an application is started a given amount of address space is allocated to the application which is called managed heap. We can make use of GetTotalMemory method of GC class to get the number of bytes currently allocated in managed memory. When a new object has to be instantiated and there isn’t enough space in the application’s managed heap, the garbage collection process runs.

 

  • Garbage collector generates a list of all the application’s objects that are still in scope.

 

  • The garbage collector steps through the managed heap, looking for any object that is not present in this list.

 

  • The garbage collector marks the objects that are out of scope. The space these objects were taking up is freed.

 

  • After the garbage collector has iterated through the managed heap, it moves the references to all the objects down to the beginning of the heap. All the object references are therefore stored contiguously in the heap.

 

 

Let’s take a simple example

 

Suppose when an application is started, objects A, B, C and D are instantiated, and references to these objects are placed on the managed heap.

 

Object D

Object C

Object B

Object A

 

Suppose later in the life of application, object E needs to be instantiated. Now there isn’t enough space in the managed heap so garbage collection needs to be performed. Objects B and C have gone out of scope.

 

When garbage collection is started, a list of all the objects that are still in scope is generated, here the list will contain object A and D, than it iterates through the managed heap, looking for the objects that are not present in the list. These objects are marked as unreachable and the managed heap is compacted. A reference to Object E is then placed on the heap.

 

 

Object D

Object C

Object B

Object A

 

To

 

 

Object E

Object D

Object A

 

 

 

Generations

 

Here in the above case it could be very expensive to step through the entire heap, looking for marked objects. To make this process more efficient, Microsoft.NET implements concept of generations. The theory of generations assumes that the older an object is, the less likely it is out to be out of scope during garbage collection. Therefore, it is most efficient to check the newest objects first and then check the older objects if space is still needed.

 

There are three generations: Generation 0, 1 and 2. When a garbage collection is performed, it collects only objects in Generation 0. If there is still not enough space to allocate a new object, then garbage collector moves on to Generation 1, and then if necessary, it moves to Generation 2 objects.

For e.g. consider an application that has object A in generation 2, B and C in generation 1 and D, E, F in Generation 0.

 

 

Object F

Gen 0

Object E

Gen 0

Object D

Gen 0

Object C

Gen 1

Object B

Gen 1

Object A

Gen 2

 

Now the application attempts to allocate space to Object G, but it cannot, so a garbage collection is required.

 

Say objects C, D and E have gone out of scope.

 

After the garbage collection, objects D and E are collected. Even though Object C had gone out of scope, the garbage collector only collected from Generation 0. If more space had been required than Object C, from Generation 1, would also have been collected.

 

And one more thing, the Object F will be moved to Generation 1 because it has survived one garbage collection. The garbage collector did not collect from Generation 1; therefore Object C is still within generation 1.

 

Now the managed heap would look something like this

 

 

 

Object G

Gen 0

Object F

Gen 1

Object C

Gen 1

Object B

Gen 1

Object A

Gen 2

  

 

Bye ..

Creating a custom Hello World web service in SharePoint


We need to do the following five steps

1) Create the web service.

2) Run disco.exe to get static disco and wsdl file for the web service.

3) Put asmx, disco and wsdl file to ISAPI folder.

4) Rename disco and wsdl file to aspx. Replace static url’s in them.

5) Add an entry to spdisco.aspx

Let’s proceed

· Create a new ASP.NET web service project.

· Rename Service.asmx to HWService.asmx.

· Right click it – select View Mark Up.

· And put the following mark up. Here we will be creating the web service with inline code.

<%@ WebService Language=”C#” Class=”MyInlineService.HelloWorldService” %>

using System.Web.Services;

namespace MyInlineService

{

public class HelloWorldService: WebService

{

[WebMethod]

public string HelloWorld()

{

return “Hello World”;

}

}

}

· Now Run Disco.exe for our HWService.asmx to create static disco and wsdl file.

· Open visual studio command prompt and run the following command

disco http://localhost:1814/HelloWorldSPService/HWService.asmx

· This will create HWService.wsdl and HWService.disco files at the current directory.

· Now copy all the three files HWService.asmx,HWService.wsdl and HWService.disco to ISAPI folder.

· Rename HWService.disco to HWServicedisco.aspx and HWService.wsdl to HWServicewsdl.aspx.

· Now we will open these new renamed wsdl and disco files to make the following modifications.

Open HWServicewsdl.aspx.

Replace

<?xml version=”1.0″ encoding=”utf-8″?>

with

<%@ 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”; %>

And

<soap:address location=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; />

To

<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

Open HWServicedisco.aspx

Replace

<?xml version=”1.0″ encoding=”utf-8″?>

with

<%@ 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”; %>

And

<contractRef ref=”http://localhost:1814/HelloWorldSPService/HWService.asmx?wsdl&#8221; docRef=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns=”http://schemas.xmlsoap.org/disco/scl/&#8221; />

<soap address=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns:q1=”http://tempuri.org/&#8221; binding=”q1:HelloWorldServiceSoap” xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

<soap address=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns:q2=”http://tempuri.org/&#8221; binding=”q2:HelloWorldServiceSoap12″ xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

with

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + “?wsdl”),Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns=”http://schemas.xmlsoap.org/disco/scl/&#8221; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q1=”http://tempuri.org/&#8221; binding=”q1:HelloWorld” xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q2=”http://tempuri.org/&#8221; binding=”q2:ServiceSoap12″ xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

· Adding it’s entry in spdisco.aspx

Open the spdisco.aspx and add the following entry

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx?wsdl”), Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx”), Response.Output); %>

xmlns=” http://schemas.xmlsoap.org/disco/scl/ />

<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx?disco”),Response.Output); %>

xmlns=”http://schemas.xmlsoap.org/disco/&#8221; />

Now your web service should be availabe to be used within sharepoint context

http://servername:port/_vti_bin/HWService.asmx.

That’s it!!

Create a simple hello world custom Application Page in SharePoint.


There are two approaches we can follow while creating an Application Page in SharePoint.

One is Inline Approach

·         Create a new default.aspx page rename it to HelloWorldAppPage.aspx

·         Put the following markup in it

<%@ Page Language=”C#” MasterPageFile=”~/_layouts/application.master” Inherits=”Microsoft.SharePoint.WebControls.LayoutsPageBase” %>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

  <asp:Label Text=”Hello World” runat=”server” ></asp:Label>

</asp:Content>

·         Every application page is an content page in SharePoint. And here to have same look and feel as the other application pages we have specified the application.master as the masterpagefile.

·         Inherits attribute of the Page directive refers to LayoutsPageBase class from which the application pages inherit.

·         We have than used a content server control that would be referring to the PlaceHolderMain content place holder inside the application.master page.

·         Than we have simple added a label control that would be displaying hello world.

·         Now go to your 12 hive i.e. C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS and create a folder (MyHelloWorld) over there and put the HelloWorldAppPage.aspx over there.

·         We could have directly placed the aspx page to layouts folder but than from better organization perspective we have created a folder and kept the aspx page inside it.

·         Now the page is available to all the SharePoint sites.

http://servername:port/_layouts/MyHelloWorld/HelloWorldAppPage.aspx

Another is code behind approach.

·         The easiest way is to first create a Asp.NET Web site project.

·         Add a class library project to the solution (GCL)

·         Delete class1.cs and copy the default.aspx.cs page from asp.net web site project to the class library project.

·         Delete the default.aspx.cs from the website.

·         Add reference to System.Web and Microsoft.SharePoint.dll  in your class library project

·         Put the following code in the _Default

public partial class _Default : LayoutsPageBase

{

    protected Label Label1;

    protected void Page_Load(object sender, EventArgs e)

    {

        Label1.Text = “Hello World Code Behind”;

 

    }

}

·         Sign the assembly and put it in GAC.

·         Get the public key token.

·         In the default.aspx page replace and put the following markup to use the assembly.

<%@ Page Language=”C#” MasterPageFile=”~/_layouts/application.master”

 Inherits=”_Default,GCL, Version=1.0.0.0,Culture=neutral,PublicKeyToken=16391a8a7c882343″ %>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

  <asp:Label ID=”Label1″ Text=”Hello World” runat=”server” ></asp:Label>

</asp:Content>

·         Put default.aspx to the  layouts folder or your custom folder inside it.

That’ s it !!

Creating a Custom Web Service in SharePoint for Uploading Document


We had a requirement to upload the document to SharePoint document library whenever a file has been attached to our entities in Microsoft Dynamics CRM. So the obvious way of doing that was to use web service within the CRM. We had already written a utility using SharePoint Object Model that would be doing so. So we thought of reusing the existing logic defined over there and wrap it inside one our custom web service’s method. We created the web service and we deployed it in our SharePoint Server but outside the context of the SharePoint i.e. as a separate web site. However when trying to use that web service from other machines we ran into security issues, it was always giving Unauthorized Access error. We tried running the web service app pool with identity of administrator and also tried out impersonation defined through web.config again passing the account of administrator. But it didn’t work

Then finally tried out this article within MSDN which guides us on creating a web service that operates within the context of WSS 3.0.

http://msdn.microsoft.com/en-us/library/ms464040.aspx

Basic steps for creating a custom web service are following

1) Create a new asp.net web service project.

2) Add a new class library project in that solution.

3) Move Service1.cs file from web service project to class library project

4) Delete the Service1.cs file in web service project and class1.cs file in the class library project.

5) Put your custom logic in service.cs file that is now in the class library project.

6) Give strong name to the class library i.e. Sign the assembly.

7) Put the assembly in GAC. Get its public key token.

8) Now make modification to the service.asmx to use the assembly.

9) Replace code behind attribute with the class attribute to refer to the assembly

<%@ WebService Language=“C#” Class=“Service, MyServiceAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8f2dca3c0f2d0131” %>

10) Generate static discovery and wsdl file.

11) Copy the .asmx file of the Web service to \Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS

12) From visual studio command prompt run the following command

disco http://MyServer:port/_layouts/MyCustomWebService.asmx

13) open both the .disco and .wsdl files and replace the opening XML processing instruction — <?xml version="1.0" encoding="utf-8"?> — 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”; %>

14) In the .disco file, modify the contract reference and SOAP address tags

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + “?wsdl”),Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns=http://schemas.xmlsoap.org/disco/scl/&#8221; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q1=http://tempuri.org/&#8221; binding=“q1:HelloWorld” xmlns=http://schemas.xmlsoap.org/disco/soap/&#8221; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q2=http://tempuri.org/&#8221; binding=“q2:ServiceSoap12” xmlns=http://schemas.xmlsoap.org/disco/soap/&#8221; />

15) In the .wsdl file, make the following changes for the SOAP address

<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

16) Rename both files in the respective formats MyCustomWebServicedisco.aspx and MyCustomWebServicewsdl.aspx so that your service is discoverable through Windows SharePoint Services.

17) Copy the new MyCustomWebServicewsdl.aspx and MyCustomWebServicedisco.aspx files, and also the MyCustomWebService.asmx fil to _Drive:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI directory

18) Open the spdisco.aspx files located in Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI and add the following code, specifying the .asmx file for your Web service.

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/MyCustomWebService.asmx?wsdl), Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/MyCustomWebService.asmx), Response.Output); %>

xmlns=http://schemas.xmlsoap.org/disco/scl/ />

<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/MyCustomWebService.asmx?disco),Response.Output); %>

xmlns=http://schemas.xmlsoap.org/disco/&#8221; />

Any modification done to the custom logic would require putting the new assembly in GAC followed by generating the static disco and wsdl file and making all the necessary modification in disco and wsdl file as mentioned in the above steps.

Now in our case finally putting the custom logic of uploading the document within the class library for the web service helped us to solve the issues that we were facing.

[WebMethod]

public string UploadDocumentToSharePoint(string fileName, byte[] fileContents, string folderName)

{

if (fileContents == null)

{

return “Null Attachment”;

}

try

{

SPWeb web = SPContext.Current.Web;

web.AllowUnsafeUpdates = true;

SPFolder folder = web.GetFolder(folderName);

string fileURL = fileName;

folder.Files.Add(fileURL, fileContents);

return “File added successfully!”;

}

catch (System.Exception ex)

{

return “Error: “ + ex.Source + “| Message: “ + ex.Message;

}

}

And this is how we used it in within our custom application after adding the web reference to it.

FileStream oFileStream = File.OpenRead(@”C:g.doc”);

byte[] content = new byte[oFileStream.Length];

// store the contents in a byte array

oFileStream.Read(content, 0, (int)oFileStream.Length);

// close the stream

oFileStream.Close();

// instantiate the service

DocService.Service docService = new DocService.Service();

// pass the current logged in user credentials

docService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

// pass the name of the doc, content and document library name

string message = docService.UploadDocumentToSharePoint(“G1.doc”, content, “CrmDoc”);

MessageBox.Show(message);

Bye…

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓