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…

Deploying a web part in SharePoint.


We need to do the following to deploy the web part.

  • First we need to sign the assembly.
  • In Visual Studio right click project and select Properties.
  • In properties, select Signing Tab.
  • Select Sign the assembly check box.
  • And specify the file or create a new file.
  • Build the assembly.
  • Now the manual way of deploying the assembly would be to put it either in GAC or bin directory of the web application and put a safe control entry within the web.config of the web application.
  • However the preferred way of deploying is through a solution file, which would make our web part available in the solution management ( global configuration section) of Central administration page, from where we can deploy to multiple web applications.
  • For this we need to create a manifest file.
  • Right click the project.
  • Add New Item – Select an xml file – name it manifest.xml
  • Put the following code on the file

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

<Solution xmlns=http://schemas.microsoft.com/sharepoint/

SolutionId=96A76C66-7258-4721-BEB4-90C06E656DB6>

                <Assemblies>

                                <Assembly DeploymentTarget=WebApplication

                                Location=DeploymentWebPart.dll>

                                                <SafeControls>

                                                                <SafeControl

                                                                Assembly=DeploymentWebPart, Version=1.0.0.0, Culture=neutral,

PublicKeyToken=b0d80600de0f4a2b

                                                                Namespace=DeploymentWebPart TypeName=*/>

                                                </SafeControls>

                                </Assembly>

                </Assemblies>

</Solution>

 

 

Soution ID – Create and assign a new guid.

DeploymentTarget attribute, which has two possible values: GlobalAssemblyCache or WebApplication.

 GlobalAssemblyCache indicates that the assembly should be deployed in the global assembly cache; WebApplication tells Windows SharePoint Services to drop the assembly in the private application folder of the IIS Web application

SafeControl element describes the configuration that must be done in the web.config file.

  • Now we will create the solution package, which is essentially a cabinet file that would contain our assembly and manifest file.
  • For this right click the project and select a CAB project from SetUp and Deployment projects folder.
  • Name it as DeploymentWebPartSolution
  • Right click the CAB Project and select – Add – Project Output.
  • In Project Output Group dialog box select the DeploymentWebPart and from the configuration drop down select Active.
  • In the project list box, select Primary Output to include the assembly.
  • Now again right click the cab project and select – Add – Project Output, this time select Content Files to include the manifest.xml.
  • Build the Project.
  • This will create DeploymentWebPartSolution.CAB file.
  • Rename it to DeploymentWebPartSolution.wsp
  • To add the solution package to solution store
  • Stsadm.exe -o addsolution -filename DeploymentWebPartSolution.wsp.
  • We now go to Solution Management link inside the Global Configuration section within Operations Tab of Central Administration Site.

There we will see our DeploymentWebPartSolution.wsp. Now click on it and select the web application where it should be deployed.

However if are creating our web part using web part project template within Visual studio 2008,  .wsp solution package automatically gets created for us when we select deploy command on right clicking the project. We can than make use of the .wsp file directly, no need to create a setup project in that case.

 

That’s it ….

 

Creating a Hello World Connectable Web Parts in SharePoint.


To write connectable web parts we need to do the following

First we need to define our own interface that will specify the data we want to pass from one web part to another.

The provider web part needs to do the following

  • Implement the interface.
  • Create a property which would be returning a reference to the interface.
  • The property should be decorated with ConnectionProvider attribute.

The consumer web part needs to do the following

  • It should contain the method which would receive the interface.
  • The method should be decorated with ConnectionConsumer attribute.

Keeping the above information in mind let’s start

 Create a new web part project within Visual Studio 2008. (HelloWorldConnectedWebPart)

Right click on the Project

Select Add New ItemàSharePointàWebPart.

Now rename the webpart1.cs and webpart2.cs class as  HWProviderWebPart and HWConsumerWebPart respectively.

Now right click the project and add a new interface class.

    public interface IStringData

    {

        string ProviderStringInfo { get; }

    }

Our Provider class should implement this interface and define one property which would be returning the reference to the interface.

[Guid(“56978c39-1958-4128-a979-b9feaf2feb46”)]

    public class HWProviderWebPart : WebPart, IStringData

    {

        public HWProviderWebPart()

        {

        }

        protected override void CreateChildControls()

        {                    

        }

        // the string info that would be passed to the consumer

        protected string myInfo = “Hello World”;    

        // implement the property defined in the interface

        public string ProviderStringInfo

        {

            get { return myInfo; }

        }     

        // create a property which would be returning the interface reference

        // decorate it with ConnectionProvider

        [ConnectionProvider(“String Provider”)]

        public IStringData ConnectionInterface()

        {

            return this;

        }

    }

 

Now let’s move to our Consumer Web Part.

[Guid(“3575c6de-e21a-4e5a-b7f0-fe1aa4844402”)]

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

    {

        public HWConsumerWebPart(){

        }

        protected override void CreateChildControls(){      

        }

 

        IStringData myProviderInterface = null;

        // The Consumer class should define a method that would accept

        // the interface as an parameter

        // Should be decorated with ConnectionConsumer attribute

 [ConnectionConsumer(“String Provider”)]

        public void GetInterface(IStringData providerInterface)

        {

            myProviderInterface = providerInterface;

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            try

            {  

                // priting the value provided by provider web part

                writer.Write(myProviderInterface.ProviderStringInfo);

            }

            catch(Exception ex)

            {

                writer.Write(“Error info “ + ex.Message);

            }

        }   

    }

 

Now build the project. ( Remove errors if any)

Right click the project.

Select Properties – Debug — Start browser with url ( Specify the site where the web part should be deployed)

Right click the project and select Deploy.

After Deploy Succeeds ,

Go to site actions — Site Settings — WebParts( Inside galleries) –Click on New– Select both the Provider and consumer web part — Populate Gallery.

Go to your home page —  Edit page — Add both the web parts –Select the provider web part — Connections and Specify the connection.

That’s it..

Understanding Web Part life cycle


Web Part Life Cycle starts with

OnInit – Configuration values set using WebBrowsable properties and those in web part task pane are loaded into the web part.

LoadViewState – The view state of the web part is populated over here.

CreateChildControls – All the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. We can make use of EnsureChildControls() – It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it.

OnLoad

User Generated Event – for e.g. button click on the web part.

OnPreRenderHere we can change any of the web part properties before the control output is

drawn.

RenderContents – Html Output is generated.

SaveViewState – View state of the web part is serialized and saved.

Dispose

UnLoad.

Bye…

Server Error in ‘/’ Application. Runtime Error on opening a SharePoint site


Got this error while opening a SharePoint site.

Changing custom error to Off it showed  the following error

Line 1:  <browsers>
Line 2:      <browser id="Safari2" parentID="Safari1Plus">
Line 3:          <controlAdapters>

Data at the root level is invalid. Line 1, position 1.

Deleted  the _vti_cnf folder of /App_Browsers/ of the site and everything was back to normal!!!

Difference between workflow created using SharePoint Designer and Visual Studio Designer for Windows Workflow Foundation.


SharePoint Designer

Visual Studio 2005 Designer for Windows Workflow Foundation.

 

Can write only sequential workflows.

Can write both sequential and state machine workflows.

 

Automatic deployment against the specific list or library against which workflow is being designed.

Can be deployed as a feature.

Logic is defined declaratively using Steps which comprises of Conditions and Actions

Logic could be defined through custom code written using C# or VB.NET.

Workflows could be associated to a specific list or library.

Workflow can be authored as Template which once deployed could be associated with any list or library.

Workflow modifications not possible.

Workflow modifications are possible using Modification forms built using ASP.NET or InfoPath form.

Workflow markup, rules all are stored as a document library on the site.

Workflows are compiled as an .NET assembly.

Can’t be debugged.

Debugging is possible using Visual Studio.

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓