Using Siebel CRM On Demand Web Services in Microsoft.NET

1) Create a new windows application.

2) Put the following code in the form load

private void Form1_Load(object sender, EventArgs e)

{

string loginUrlString = https://servername/Services/Integration?command=login”;

String sessionID = ManageSession.Login(loginUrlString, @”orgname-devusername”, “password”);

}

3) Define the login method within ManageSession class in the following manner


public static string SessionID = “”;

public static String Login(String loginUrlString, String userName, String password)

{

try

{

// create a http request and set the headers for authentication

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrlString);

HttpWebResponse myResponse;

myRequest.Method = “POST”;

// passing username and password in the http header

// username format if it includes slash should be the forward slash /

myRequest.Headers[“UserName”] = userName;

myRequest.Headers[“Password”] = password;

myResponse = (HttpWebResponse)myRequest.GetResponse();

Stream sr = myResponse.GetResponseStream();

// retrieve session id

char[] sep = { ‘;’ };

String[] headers = myResponse.Headers[“Set-Cookie”].Split(sep);

for (int i=0; i <= headers.Length-1; i++)

{

if (headers[i].StartsWith(“JSESSIONID”))

{

sep[0] = ‘=’;

SessionID = headers[i].Split(sep)[1];

break;

}

}

sr.Close();

myResponse.Close();

}

catch (WebException webException)

{

}

catch (Exception ex)

{

}

// send back the session id that should be passed to subsequent calls

// to webservices

return SessionID;

}

That’s it..

The remote server returned an error: (500) Internal Server Error while using Siebel CRM On Demand Web Services.

I got this error while trying to Logging In to the Web Services Session using .NET. Spent whole day for finding out what could be the reason behind the issue. Finally came to know the issue was because of the username being not passed correctly.

 

The username that we were passing was in this particular format that we would normally do

 

 ‘orgname-devusername’

 

Whereas it was expecting it in this format

 

‘orgname-dev/username’

 

i.e. forward slash instead of the back slash.

 

I hope it helps!

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!!