Monday, October 29, 2012

SharePoint 2013 REST Service

SP 2010 is the first version of SharePoint which supports REST API/Service (REpresentational State Transfer). SP 2010 has REST located at http://[web site URL/_vti_bin/ListData.svc.

SharePoint 2013 has few changes related to REST access URL. SP 2013 has REST located at http://[web site URL]/_api/.

SharePoint 2013 REST endpoints map to SharePoint Client object model types and members. As we know that, SharePoint Client Object Model allows access to various objects in the hierarchy down from the Site Collection i.e. Site (Site Collection), Web (Web Site), List, ListItem etc.

Similarly, the SP 2013 REST allows access to object Site and the objects below it in the hierarchy.

SP 2010 REST API was allowing access to only lists and their items in a web site. SP 2010 REST never given access to Site or Web objects whereas SP 2013 REST can access these objects as well.

Let us see how REST works and how the REST endpoints are formed.


Access Site Collection Object and its properties

http://[Web Url]/_api/site

Use above URL format to access the site collection object in Atom feed format. Do you re-collect “Site” object in Client Object Model representing site collection? Here we used the same object after “_api/”.

Web URL can be any web site in the site collection. Site object can be accessed from any web site in the site collection.

Look at the below XML response for the site collection object.


 
There are two XML namespaces “m:” and “d:” representing metadata and data namespaces respectively.  Complex types and collections are represented as Atom feeds in REST and belong to default namespace. The properties with primitive types belong to “<m:properties>” node.

The “term” attribute on the “<category>” node has value set to fully qualified name of the type of Client Object Model.

The <link> node represents the collections and complex type properties of the object. The “href” attribute provides the /site relative URL to access the atom feed for collection/complex type.

e.g. To access Root Web of the site collection, use

http://[web URL]/_api/site/RootWeb

Similarly one can access the primitive properties by appending the name property in URL after “/site/”. The response is not in Atom feed format. It is just a XML fragment. e.g. To know if the site collection allows use of SharePoint Designer, use

http://[web URL]/_api/site/AllowDesigner

The properties presented in REST response do not contain all properties of “Site” type. e.g “Site” has property “Usage”. There is no <link> node for it in the response, but we can access the property using below URL.
http://[web URL] /_api/site/usage

Access Web site and its properties
http://[Web Url]/_api/web

The above REST endpoint lets us access the basic Web object of Client Object Model.
 
To read web “Title”, use

http://[web URL]/_api/web/title
To read lists collection in a web site, use

http://[web URL]/_api_web/lists

The response from above REST endpoint contains nodes for each list/library in the web site. You can access the specific list, using the <id> node value inside the specific list’s entry.

To read the specific list details, use
http://[Web Url]/_api/web/Lists(guid'[Guid of the list]')

To access the items of a list, use
http://[Web Url]/_api/web/Lists(guid'[guid of the list]')/Items

To access an item with specific ID, use
http://[Web Url]/_api/web/Lists(guid'[guid of the list]')/Items([id of the item])

Thursday, October 4, 2012

Related List /Items View in SharePoint 2010

SharePoint 2010 has an enhancment to show related list views. There is pre-requisite to make it work. You should have lists with relation established using lookup columns.

Let's try it.

1. Create a custom list named "Country". We will use "Title" column. If you want to add any new columns, you can do so.

2. Create one more list named "City". Create the columns as shown below. The "Country" must be a lookup column to "Country" list.

3. Add some city information to the list.

4. Right-click on any city and click "Open in New tab/Window". This way it will launch the display form in browser window. This will make "Site Actions" menu available. The "Site Actions" menu is not available in dialog. Click "Site Actions" -> Edit Page.

Click inside the existing web part zone. The ribbon starts presenting "Page Tools" tab. It has a button "Related List". Click it. It shows the all related list. In our case, it is city.

5. Click on the related List "City". It added a related items view as shown below. It will only show cities for the selected country. i.e. the country for which display form is shown.

6. Do not forget to click "Stop Editing" in the "Page" tab of ribbon. This actually saves the page with changes.

7. User can add items to the related list using related items web part. Click the "Add new item" link and it starts showing add new item form page for the "City" list. You can add item here. If the Country display form (View Properties) is being preseneted for "India", still you can add item for "Japan" from this screen. Be careful.


8. You can change the view of related list items. You can add/ remove column from the view using "Edit the current view" link or selected the appropriate view from the drop-down.

9. Edit the current view, after saving view changes takes back to the display form which will show nothing. So close the browser tab/window and reopen. You can see the changes.

Using SPD 2010:

1. Create a new view "DetailedView" for Country list. The view must be same as All Items view.
2. Check-out the page in SharePoint Designer 2010. To accomplish this, open the list using All Files option in SPD 2010.
3. Right-click the page and click on "Edit in Advanced mode".
4. In "Design" view, click inside the available web part zone. Put the cursor at a location where you want to add the related list items web part.
5. Click "Insert" tab in ribbon and it has a button called "Related Item View". This button presents all related lists as shown below.

6. Click the "City:Country" and it adds the related items view on the page as below. Save the page and check -in
7. The view page on browser refresh starts showing a new column as shown below to select an item and the related items are shown for the selected item. Selected item has dark black arrow icon.
 


I will suggest to use browser based method as it will avoid the un-ghosting of the page. But if you would like to customize the XSL of related items web part, better to use SharePoint Designer.