Software Architecture-Sharepoint Server 2010-First try with RESTFULL service – ListData.svc

May 29, 2011

First try with Sharepoint RESTFULL service.

Sharepoint expose few RESTFUL services, one of them is ListData.svc service that returns a standard Atom service document that describes collections of information that are available in the SharePoint Server site.
Normally, URL for RESTFULL service is as:
http://SERVERNAME/SITECOLLECTIONADDRESS/_vti_bin/ListData.svc
Such as:
http://win2k8-ultimate/sites/Call/_vti_bin/ListData.svc

When I, for the first time tried to access service, I got following error as:

Could not load type 'System.Data.Services.Providers.IDataServiceUpdateProvider' from assembly 'System.Data.Services, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

While googling about it, I came to know that it is a well known error and Web is full of its references.
Solution is to install ADO.NET Data Services Update for .NET Framework 3.5 SP1 located at:
http://www.microsoft.com/downloads/en/details.aspx?familyid=79d7f6f8-d6e9-4b8c-8640-17f89452148e&displaylang=en
After install, restart of machine, I browsed the service successfully and output was as below:

That was my first encounter with Sharepoint RESTFULL Service……:)

Advertisements

Software Architecture-Sharepoint Server 2010-Developer Dashboard-Off-On-OnDemand

May 22, 2011

Microsoft has provided a tool to trace and diagnose the creation of sharepoint page via Developer Dashboard. It looks like as below:

3 Methods to enable/disable sharepoint developer dashboard are as:

1. Via c# Code in a application.

      SPDeveloperDashboardSettings settings =
        SPWebService.ContentService.DeveloperDashboardSettings;
      settings.DisplayLevel = SPDeveloperDashboardLevel.On;
      settings.TraceEnabled = true;
      settings.Update();

DisplayLevel value can be On,Off or OnDemand as per requirement.
OnDemand means that a icon will be provided at the top of page for changing the Dashboard b/w On or Off.

2. Via Powershell

$DevDashboardSettings = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$DevDashboardSettings.DisplayLevel = 'OnDemand';
$DevDashboardSettings.RequiredPermissions = 'EmptyMask';
$DevDashboardSettings.TraceEnabled = $true;
$DevDashboardsettings.Update();

DisplayLevel value can be On,Off or OnDemand as per requirement.

3. Via stsadm

STSADM –o setproperty –pn developer-dashboard –pv Off

Last parameter can be On,Off or OnDemand as per requirement.


Software Architecture-Sharepoint Server 2010-First piece of code with SPSite and SPWeb

May 22, 2011

Environment: Visual Studio 2010 + Sharepoint Server 2010

As part of career direction, I have decided to include Sharepoint as my skillset. So I explored the Sharepoint as end user for the last few weeks. Today I have programmed first piece as sharepoint developer…..Hurrahh..As I had experienced with many other CMS solutions like PHPNuke,Joomla,DotnetNuke and custom build. So basic concept was not new while exploring the sharepoint.

My First Program was targeting to explore the Server Side Object Model specially the class of SPSite which represent the site collection in Sharepoint and SPWeb which target a web site.

      const string siteUrl = "http://win2k8-ultimate/sites/Call";
      using (SPSite siteCollection = new SPSite(siteUrl)) {

          foreach (var item in siteCollection.AllWebs)
          {
              Console.WriteLine("Item of Site collections: " + item.ToString());
          }
        SPWeb site = siteCollection.RootWeb;
        Console.WriteLine("Root Site:" + site.Name + " " + site.PortalName);
          //Gets or sets the description for the site.
        Console.WriteLine("Desc: " + site.Description);
        /// Gets a list of the groups that are associated with the website.
        foreach (var item in site.AssociatedGroups)
        {
            Console.WriteLine(" Related Group: " + item.Name);
        }
        //Gets a collection that contains all the groups in the site collection.
        foreach (var item in site.SiteGroups)
        {
            Console.WriteLine("Sitegroup Name: "+ item.ToString());
        }
        //Gets the users who have been given contribute permissions to the website.
        Console.WriteLine(" Member Group: " + site.AssociatedMemberGroup.Name);
        //Gets or sets the associated owner groups of the website.
        Console.WriteLine(" Owner Group: " + site.AssociatedOwnerGroup.Name);
        //Gets or sets the associated visitor group of the website.
        Console.WriteLine(" Vistor Group: " + site.AssociatedVisitorGroup.Name);

        //Gets the collection of all lists that are contained in the website.
        foreach (SPList list in site.Lists) {
          if (!list.Hidden) {
            Console.WriteLine("Items of List: "+list.Title);
          }

          //Gets the collection of all list definitions and list templates that are available for creating lists on the website.
          foreach (SPListTemplate item in site.ListTemplates)
          {
              Console.WriteLine("Item of List templates: " + item.Name);
          }
            //Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.
          foreach (var item in site.AllUsers)
          {
              Console.WriteLine("User of site:( Direct + via Group ) " + item.ToString());
          }
          //Gets the collection of user objects that are explicitly assigned permissions in the website.
          foreach (var item in site.Users)
          {
              Console.WriteLine(" USer of site:(Only Direct users) " + item.ToString());
          }
          //Gets the collection of all users that belong to the site collection.
          foreach (var item in site.SiteUsers)
          {
              Console.WriteLine("user of whole site collection:" + item.ToString());
          }
            //Gets the collection of the website administrators.
          foreach (var item in site.SiteAdministrators)
          {
              Console.WriteLine(" site admins: " + item.ToString() );
          }

            //Gets the collection of Features that are currently activated in the website.
          foreach (var item in site.Features)
          {
              Console.WriteLine("Feature of site: " + item.Definition.DisplayName);
          }
          //  Gets the collection of field objects that represents all the fields in the website.
          foreach (var item in site.Fields)
          {
              Console.WriteLine("feild of site:" + item.ToString());
          }
            //Gets the collection of all files in the root directory of the website.
          foreach (var item in site.Files)
          {
              Console.WriteLine("files of site: "+ item.ToString());
          }
          //Gets the collection of all first-level folders in the website.
          foreach (var item in site.Folders)
          {
              Console.WriteLine("Folders of site:"+ item.ToString() );
          }
            //Gets the collection of modules that are used on the website.
          foreach (var item in site.Modules)
          {
              Console.WriteLine("module of site: " + item.ToString());
          }
        }
        Console.ReadLine();
      }

Comment s explains everything in the above code.
Note:
1. Visual Studio project by default target .Net 4 framework. It needs to be changed to .Net 3.5 framework.
2. Project properties >> Build >> Target plateform needs to be changed to x64.
3. User which is running to code must be site collection administrator, otherwise Access denied exception will be thrown by code.


Software Architecture – RESTful WCF Services for .Net Framework 4.0/Visual Studio 2010

May 14, 2011

According to Wiki, Restful Webs services are described as:

A RESTful web service (also called a RESTful web API) is a simple web service implemented using HTTP and the principles of REST. It is a collection of resources, with three defined aspects:
•the base URI for the web service, such as http://example.com/resources/
•the Internet media type of the data supported by the web service. This is often JSON, XML or YAML but can be any other valid Internet media type.
•the set of operations supported by the web service using HTTP methods (e.g., POST, GET, PUT or DELETE).

For programming Restful services in .Net Framework, till visual studio 2008 sp1, there was a separate kit for named “WCF REST Starter Kit Preview 2” at:
http://aspnet.codeplex.com/releases/view/24644

But it was not upgraded for Visual studio 2010. So for Visual studio 2010/.Net 4.0, search WCF+REST related items in Visual studio gallery.

http://visualstudiogallery.msdn.microsoft.com/site/search?f%5B1%5D.Type=SearchText&f%5B1%5D.Value=WCF%20REST%20Service&f%5B0%5D.Value=templates&f%5B0%5D.Type=RootCategory

One of the best template by WCF Team in Microsoft is as below:

http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd

Download file named “WcfRestService40(CS).vsix”, and double click to install the template.

Now a new project type named “WCF REST Service application” is added.

I created a new project based on new project type named “WcfRestServiceApp”.

Added a console project to solution for running service named “ServiceServerApp”, with following code

WcfRestServiceApp.Service1 serv = new WcfRestServiceApp.Service1();
            WebServiceHost host = new WebServiceHost(serv, new Uri("http://localhost/WCFRESTService/"));
            host.Open();
            Console.WriteLine("Service is running....Press enter to stop the service...");
            Console.Read();
            host.Close();

When you try to run service, you will get following error:

In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single.  This can be configured via the ServiceBehaviorAttribute.  Otherwise, please consider using the ServiceHost constructors that take a Type argument.

As error explains, I have to change InstanceContextMode from InstanceContextMode.PerCall
to InstanceContextMode.Single

After that I got following error:

HTTP could not register URL http://+:80/WCFRESTService/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

So solution is to run following command incase of windows 7.

netsh http add urlacl url=http://+:80/WCFRESTService/  user=juliet\shah

For theory details see the link above.

So I am ready with service running .

While analyzing code of template, two things are important to note.
•Template uses the WebGetAttribute and WebInvokeAttribute to associate service operations with both GET and other HTTP verbs like PUT, POST, and DELETE.
•Template uses the UriTemplate and UriTemplateTable classes to provide URI processing capabilities.
For details about theory visit following
http://msdn.microsoft.com/en-us/library/bb412172.aspx

That all from my side for RestFull web Services in .Net framework 4.0/Visual studio 2010