Pages

Wednesday, March 9, 2011

SharePoint 2010 Client Object Model: Manipulate Choice, Lookup field

After my few posts on Client Object Model, I had come to questions on how to manipulate choice and lookup field. I’ve tried to explain a bit on how you can manipulate these field values with Client Object Model.

 

Manipulate Choice Field Value (Single Choice)

You can manipulate the single choice field value as like string. For example, let’s consider a field, ProductStatus in Product list. The field values might be “In Stock, Out of Stock, Invalid” as shown below:

image

Figure 1: Single Choice Field (ProductStatus) in product list.

To access the value of the field using Client Object Model, you can use code shown below:

  • Get Field value: You can just get the field value as string
    var productStatus = productItem["ProductStatus"].ToString();

  • Set Field value: You can use any of the following statement to set the field value
    productItem["ProductStatus"] = "In Stock";
    productItem["ProductStatus"] = "Out of Stock";
    productItem["ProductStatus"] = "Invalid";

Manipulate Choice Field Value (Multiple Choice)

If the choice field support multiple values then you need to use string array to manipulate field values. For example, consider there’s a field ‘product types’  in product list whose values can be Foods, electronics, Cars etc. Also consider the field values can be multiple, that’s mean a product types can be more than one type. The following figure shows the field

image

Figure 2: Multiple Choice Field

In that case you need string array to access the multiple choice field value as shown as shown below:

  • Get Field Value:
    var productTypes = (string[]) (productItem["ProductType"]);

  • Set Field Value:
    productItem["ProductType"] = new string[] { "Furniture", "Toys" };

Manipulate Lookup Field Value

To manipulate lookup field you need to use the code as shown below:

  • Get Field Value:
    var lookupFieldValue = (productItem["FieldName"] as FieldLookupValue);

  • Set Field Value
    //100 here is the lookup field id value
    productItem["FieldName"] = new FieldLookupValue(){LookupId = 100};

The FieldLookupValue is part of SharePoint Client OM .

Sunday, March 6, 2011

SharePoint 2010: Create Custom WCF Service

In SharePoint 2007, creating a custom Web Service was not so easy. However, asp.net web services are obsolete in SharePoint 2010. Rather new and recommended approach is to develop WCF Service. So the question comes up, “How much difficult it is to create a custom WCF service in SharePoint 2010?”. I’m going to answer the question just right in this blog.

 

Install CKS development tools edition

For showing how easily you can develop your own Custom WCF Service in SharePoint 2010, I’m going to use a open source Visual Studio 2010 extension know as Community Kit for SharePoint: Development Tools Edition. This tool will make the WCF service development much easier. It’ll automate tasks that you would have to do manually. There are two version of the extensions: One for SharePoint Foundation and another one is for SharePoint Server. Download the appropriate version and install.

 

Create WCF Service

Once you installed the CKSDev Visual Studio extension, you can open a SharePoint Project. In the SharePoint Project, right click on the project and try to add a new item. In the “Add New Item” dialog, you will find some new items added by CKSDev Visual Studio extension. Please select the option “WCF Service (CKSDev)” for new item as shown below:

image

Figure 1: ‘Add New WCF Service’ option ‘add new item’ dialog

 

Once you add the WCF Service, two files will be added by the dialog. One is the service interface and another is the Service itself.

 

Modify Service Types

As defined in MSDN, there are three different service types. Most of the time you need SOAP service. But if you need REST or ADO.NET Data service you can modify the service types by modifying the service factory as sown in the figure 2. The following table shows the three service types and their service factory name.

Service Type

Service Factory

Description

SOAP service

MultipleBaseAddressBasicHttpBindingServiceHostFactory

Basic HTTP binding must be used, which creates endpoints for a service based on the basic HTTP binding.

REST Service

MultipleBaseAddressWebServiceHostFactory

The service factory creates endpoints with Web bindings.

ADO.NET Data Service

MultipleBaseAddressDataServiceHostFactory

A data service host factory can be used.

When you create service with CKSDev tool, the default service generated is SOAP service. If you want to change the service type, please modify the factory in .svc file as shown below:

image

Figure 2: Service Factory defined in SVC file.

 

Deploy the Service

Once you are done with the service development, you are ready to deploy. But where you want to deploy the service? By default SharePoint service are kept in ISAPI directory. However, CKSDev deploy the service in ISAPI\ProjectNameSpace path as shown below:

image

Figure 3: Service deployment location

Once you define the service deployment location as shown in the figure 3, you can deploy the solution.

 

Access the Custom WCF Service

After Service deploy, you need to use the service in another projects. First try to access the service in browser. But remember you need to access the MEX endpoint either you will not get the service accessible in browser. To access the MEX endpoint, you should add “/MEX” at the end of the service name as shown below:

image

Figure 4: Access WCF Service MEX endpoint.

 

Finally try to add the service reference in a project using Visual Studio’s ‘Add Service Reference’ dialog as shown below:

image

Figure 5: Add Service Reference

 

 

Conclusion

So the steps described in this post are pretty simple:

  • Make sure you have downloaded and installed CKSDev Visual Studio extension.
  • Create a WCF Service (CKSDev) in the project. And if necessary, modify the service type
  • Deploy the solution and if necessary, change the deployment path.
  • Access the service MEX endpoint.

You are done. Pretty simple, I think.

Wednesday, March 2, 2011

SharePoint 2010: Select the best option for accessing SharePoint data from clients

There are few ways you can access data stored in SharePoint from a non-SharePoint application.

  • Client Object Model (OM): SharePoint provides three flavors of Client OM (Managed, EcmaScript and Silverlight)
  • Asp.Net web Service: The legacy web services of SharePoint 2007 are still supported for backward compatibility. We should try to avoid using these legacy web services for green field development.
  • REST-based Service (SharePoint 2010 Provided): SharePoint 2010 provides new set of WCF service which is REST enabled.
  • Custom WCF Service: There’s another option of developing custom WCF service of your own.

Let’s discuss which options you’ll take into account in selecting the best suitable options for your applications:

 

Client Object Mode (OM)

I think Client Object Model is the best choice in most of the cases. If you want to access SharePoint data from SharePoint webpart, then you can use Client OM (EcmaScript) to access data in SharePoint. If you are trying to access SharePoint data from Silverlight then you can use  Client OM for Silverlight. And most of all you can use Managed Client OM, in supported .net language to access SharePoint data.

So Client OM is surely your first choice. But in many cases you can’t use Client OM, especially in cases where you are trying to access SharePoint data from non-Microsoft platform, like Java. Also if your migrating your application from SharePoint 2003/2007 and you are already using asp.net web service, then you don’t have much choice but to use the legacy web services.

 

Asp.Net Web Services

SharePoint 2007 comes with built-in web services. These legacy web services are still in SharePoint to support backward-compatibility but whenever you have the option to avoid them, please do so. The only reason I see to use these legacy web services is for applications migrating from pre-SharePoint 2010 to SharePoint 2010. Maybe this is the last version of SharePoint with the support of the legacy web services (at least I hope so). Few legacy asp.net services are listed below:

  • /_vit_bin/Lists.asmx
  • /_vit_bin/Copy.asmx

 

SharePoint WCF Services (REST-based)

The new addition of extensibility point in SharePoint 20l10 is WCF services. If you are planning to manipulate SharePoint data from different platforms, like Java, then these WCF services are the excellent option to go with. If you install WCF data service updates, then these WCF services enable REST-based request processing. For more information on REST-based interface of SharePoint WCF services, please follow the MSDN link. You can also use these WCF services from .net applications. You can get the help from MSDN on how to use these WCF services in .net applications. Few new WCF services available in SharePoint 2010, are listed below:

  • /_vti_bin/ListData.svc
  • /_vti_bin/Client.svc

 

Custom WCF Service

In real world, we rarely happy with out of the box functionalities. We need to customize a lot to happy our clients. Similarly we may need to develop our own WCF services. Writing a custom asp.net web service in SharePoint 2007 was a very difficult task. But in SharePoint 2010, writing a custom WCF service is much easier now. If you install Community Kit for SharePoint (CKS) Visual Studio extension from CodePlex, you can develop a custom SharePoint Service easily. There’s also manual process of developing custom WCF Server in SharePoint described in MSDN: WCF Services in SharePoint and Creating Custom WCF Service. I’ll try to post more details on how to use CKS add-in to develop custom WCF service in another post.

Tuesday, March 1, 2011

SharePoint 2010: Approve/Reject Content Programmatically with SharePoint Object Model

In SharePoint 2010, you can modify the moderation status (approve/reject) of an item programmatically. Once you have got the ListItem, you can access the ModerationInformation properties to know the status, as shown below:

SPListItem listItem = GetListItem();
var moderationInformation = listItem.ModerationInformation;
if (moderationInformation != null)
{
if (moderationInformation.Status == SPModerationStatusType.Approved)
{
//approved
}
else if (moderationInformation.Status == SPModerationStatusType.Denied)
{
//rejected
}
else if (moderationInformation.Status == SPModerationStatusType.Draft)
{
//item is in edit mode and yet send to pending state.
}
else if (moderationInformation.Status == SPModerationStatusType.Scheduled)
{
//approval is waiting to be processed by a timer service.
}
}

However, Remember to check the ModerationInformation for null. If versioning/content approval is not enabled then the moderationinformation will be null. You can modify the moderation status by editing the moderationinformation and updating the list item as shown below:

SPListItem listItem = GetListItem();
if (listItem.ModerationInformation != null)
{
listItem.ModerationInformation.Status = SPModerationStatusType.Approved;
listItem.ModerationInformation.Comment = "This is comment";
listItem.Update();
}

Hope someone will get this useful!