Month: February 2012

Using REST API to access a Sharepoint List


Hello again 🙂

First of all let’s define what rest stands for ! In fact REST (REpresentational State Transfer) embodies the idea of accessing data across the Internet network, referencing resources using a clear and unique syntax.

Still a bit vague i know !! But you will get it at the end of this post i promise 😉

in this walkthrough we are going to access a sharepoint List from the browser and then from a console application using Visual studio 2010.

So Lets Get Started !!

1. Defining the List:

I almost always use the same list in my post. So here it is again my favorite one Employee Review:

you can create it or use another list as you like.

2. Accessing the list from the browser:

In SharePoint 2010 we can access all SharePoint List  via Restful Service.  Here is the URL to access List REST Service.

http://YourServer/SiteUrl/_vti_bin/ListData.svc.

For Example: my URL becomes : http://win-5g4msqldlt8/_vti_bin/ListData.svc

as you can see here i have all the lists in my site.

so now i want to see a particular list (Employee Review): My URL becomes : http://win-5g4msqldlt8/_vti_bin/ListData.svc/EmployeeReview

See how simple it is !!! and you can do all sorts of queries via URL.

3. accessing the list from a console application:

1. So lets start by opening Visual studio 2010.

2. create a console app project named Employee Review:

3. Right click on the References node in the Solution Explorer window, then click Add Service Reference.

4. Enter http://win-5g4msqldlt8/_vti_bin/ListData.svc for the address.  then click GO and you will have all the lists.

5. Change the namespace from ServiceReference1 to Review.

6. now we are ready to code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using  Employee Review.Review;

namespace Gears
{
class Program
{
static void Main(string[] args)
{
TeamSiteReviewContext dc =
new TeamSiteReviewContext(new Uri(“http://win-5g4msqldlt8/_vti_bin/ListData.svc”));
dc.Credentials = CredentialCache.DefaultNetworkCredentials;
var result = from d in dc.EmployeeReview
select new
{
Name = d.EmployeeName,
Rating = d.Rating,
Comments = d.Comments,
};
foreach (var d in result)
Console.WriteLine(d);
}
}
}

and Voila you will have all the list in you console !! Good job 😉

Creating a site Workflow with Sharepoint Designer


Hello 🙂

In this walkthrough, i am going to show you how to create a Site Workflow that Writes to a Custom List !! sounds complicated but trust me it is not 😉

So lets Get started 😉

The following example uses a site workflow to capture employee reviews submitted by employees,
which their managers will then need to review and approve.

To do this, you’ll create a custom list, customize the fields on the form that are used to start the
workflow, look up the manager of the person submitting the workflow, write it all to a list, and then
assign a task to the manager. Follow these steps:

1.Open your site in SPD.

2. Click the Workflows link in the Navigation pane on the left.

3. From the ribbon, click the Site Workflow button to create a new workflow.

4. Name the workflow Employee Reviews and click OK. The Workflow Designer for the new workflow will automatically open.

5. With the Workflow Designer open, you should see a blinking horizontal orange line, which is used to indicate where the next action or condition will be inserted. But, before you add any steps to this workflow, from the ribbon, click the Initiation Form Parameters button shown in the Figure:

This allows you to define parameters that are used to collect data when the workflow is started. In this case, you want the user to fill out the form and then use the information that was entered throughout the workflow.

6. From the “Association and Initiation Form Parameters” dialog, click the Add button.


7. Name the field Employee Name, choose “Single Line of Text” as the Information type, and click Next. Leave the default value blank and click Finish.

8 Once you’ve completed adding the fi rst parameter you should be returned to the “Association
and Initiation Form Parameters” dialog. You need to create another parameter, so click the Add button and name the field Rating. Choose Choice as the Information type (that is, a menu to choose from), and click Next. On the ColumnSettings screen, enter the values shown in the Figure  and click Finish.


9 Click the Add button to add another field. Name this field Comments, set it as “Multiple lines of text,” and click Next. Then click Finish. Click the OK button from the “Association and Initiation Form Parameters” screen.

10 The Workflow Designer should open and there should be a horizontal, orange blinking cursor in a box titled Step 1. From the ribbon, click the Action button and then click “Send an Email” from under the Core Actions section, as shown inthe Figure.

Optionally, instead of clicking the Actions button, you can start typing and SPD will try to figure out what you want it to do.

11 The new action will be added to the workflow, but it still must be configured. To do that, click the “these users” link in the action, which will open the Define E-mail Message dialog.

12 Click the phone book icon at the right of the “To:” field to open the Select Users dialog. Select “Workflow Lookup for a user”, then click the Add button. Choose the following values from the “Lookup for Person or Group” dialog shown in the Figure :

➀➀ Data Source — Workfl ow Context
➀➀ Field from source — Initiator
➀➀ Return field as — Email

Once complete, click OK and you’ll be taken back to the Select Users dialog. Click OK again on this screen to
define the rest of the fields in the e-mail.

13. For the “Subject” field, enter the text Employee Review Received. Optionally, here you could also click the ellipsis (
) button to open the string builder to create a dynamic subject that could pull values from the user, or from the workflow itself.

14. In the body field of the e-mail, enter the text Your review has been sent to your manager who will schedule a meeting within the next week, as shown in the Figure .

Again, you can optionally click the “Add or Change Lookup” button in the bottom-left corner of the dialog to add dynamic text from the workflow itself. When you are finished, click the OK button to return to the Workflow Designer.
15. From the ribbon, click the Action button and select Create List Item. This adds the action to
the workflow. To configure it, click the “this list” link in the action, which opens the Create
New List Item dialog.

16. Click the Add button to open the Value Assignment dialog. Select the field to set as Employee Name. Then, to assign the value, click the function button. Choose the following values:

➀➀ Data Source — Workflow Variables and Parameters
➀➀ Field from source — Parameter: Employee Name
➀➀ Return field as — As String

When you have finished, click OK, and then click OK again to return to the Create New List Item dialog.
17. Repeat Step 16 to add the Rating and Comments fields from the parameters captured in the form, as shown in the Figure. Once you have added all of the fields, click OK.

18. Back in the Workflow Designer, click the Action button again and choose “Assign a To-do Item.”

19. Click the link for “a to-do item” and the Custom Task Wizard will open. Click Next. On the second
page of the wizard, enter Employee Review in the Name field and click Finish.

20. Next, from the Workflow Designer, click the “these users” link in the “create to-do item” action that you’ve been editing. From the Select Users dialog, click “Workflow Lookup for a User,” and then click Add. The “Lookup for Person or Group” dialog window will open.
21. Set the following values for the lookup in the top section of the window:

➀➀ Data source — User Profiles
➀➀ Field from source — Manager
➀➀ Return field as — Login Name

22. For the fields at the bottom of the “Lookup for Person or Group” dialog shown in the Figure,


choose Account Name from the drop-down menu for the Field value. Next, click the function button
to the right of the Value field. In the dialog that opens, set the fields as follows:

➀➀ Data source — Workflow Context
➀➀ Field from source — Initiator
➀➀ Return field as — Login Name
The name of the manager is being returned for the purpose of assigning a task to the manager,
which is why the Login Name is used as the return field.

23. Click OK on all the dialogs to accept the changes.
24. The workflow is now complete and should look similar to this  Figure:

From the ribbon,click the Save button. Before the workfl ow can be used, it must fi rst be published. To do so,
click the Publish button on the ribbon, as shown in the Figure.

You’re done !!! 🙂

To try the site workflow, go back to your site and click Site Actions âžȘ View All Site Content. At the top of the page, click the Site Workflows link.

Click the Employee Reviews link, as shown in the Figure, which opens the page where you can fill out the form to start the workflow. Fill out the form and click Start. To make this form easier to access, you can copy the form’s URL and create a link directly to the form.

You can see the workflow has kicked off by clicking Site Actions âžȘ View All Site Content, and then clicking the Site Workflows link. The workfl ow task will be listed under the My Running Workflows section.
Click the link for the workflow to go to the Task Item that’s been created. You should see that the task has been assigned to the manager of the user who started the workflow.

Hover your mouse over the Title of the workflow task item and select it to edit the task item. If you log in as a user who can edit the task, you can click the “Complete task” button. When you view the status of the workflow, the outcome should show as Completed.
If you go back to the Employee Review list, you’ll see that a new item has been created and the information entered into the form has been added, as shown in the Figure:

See U next time 😉

Linq To Sharepoint for Beginners


Hello 🙂

In SharePoint 2010 you now have the ability to use LINQ syntax to fetch items from your lists instead of using the “traditional” approach of CAML queries.

In this Post I will show you how you can start using LINQ queries in SharePoint, also known as LINQ to SharePoint.

So let’s Get Started 😉

1. creating a sample Web Part 

first of all open Visual studio 2010 as an admin.

now Create an empty Sharepoint project and name it LinqToSharePointProject.

Right Click on the project choose add and New Item

choose VisualWebPart and Name it LINQWebPart

now : Add proper references; in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linqassembly. Point to references, right-click and choose “Add Reference” and select the Microsoft.SharePoint.Linq.dll file:

Add the following statement to the top of the LINQWebPartUserControl.ascx.cs/vb file to reference the necessary assemblies:

2. Using SPMetal.exe :

Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.

First launch a cmd-window and navigate to C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14bin by using cd C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

and then run the following command to utilize the SPMetal.exe tool with the following syntax:

SPMetal /web:http://yousite /code:Entities.cs /language:csharp

for example: SPMetal /web:http://win-5g4msqldlt8 /code:C:\Entities.cs /language:csharp

Locate the file named Entities.cs that was created in the previous step to make sure that is was created.

now back to visual studio to add this file to the project.

Add the Entities.cs file you created in the previous exercise to the project by right-clicking the project and selecting Add » Existing Item.

choose  the file Entities.cs.

3. Ready to code?

Up until now We:

  1. Generated the entities using the SPMetal.exe tool
  2. Referenced the newly created file from the SharePoint project
  3. Made sure we’re using the proper references for System.Linq and Microsoft.SharePoint.Linq
  4. Be ready to code:-).

Example : i created a simple list for this demo ( before i genereted the entities file of course). it’s named Employee Review. you can create this list or you can use any list available to you.

now we are going to Write the code for the Visual WebPart User to access the list data.

In Visual Studio 2010 open the LINQWebPartUserControl.ascx file from the Solution Folder by double-clicking on it. then  Add the following markup under the <%@ Control
 declaration

<%@ Import Namespace=”Microsoft.SharePoint.WebControls” %>
<SharePoint:SPGridView id=”spGridView” runat=”server” AutoGenerateColumns=”false”>

<HeaderStyle HorizontalAlign=”Left” ForeColor=”Navy” Font-Bold=”true” />

<Columns>

<SharePoint:SPBoundField DataField=”Title” HeaderText=”Title”></SharePoint:SPBoundField>

<SharePoint:SPBoundField DataField=”Rating” HeaderText=”Rating”></SharePoint:SPBoundField>

<SharePoint:SPBoundField DataField=”Comments” HeaderText=”Comments”></SharePoint:SPBoundField>
</Columns>

</SharePoint:SPGridView>

In the Solution Explorer, expand the carat icon next to theLINQWebPartUserControl.asx, and then double-click on theLINQWEBoPartUserControl.ascx.cs file to open it.

Add the following code in the existing Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
try { using (EntitiesDataContext dc = new EntitiesDataContext(“http://win-5g4msqldlt8&#8221;))
{
EntityList<EmployeeReviewItem> Employees = dc.GetList<EmployeeReviewItem>(“Employee Review”);
var empQuery = from emp in Employees

select new
{
emp.Title,
emp.Rating,
emp.Comments
};

spGridView.DataSource = empQuery;
spGridView.DataBind();
}
}

catch (Exception x) { }
finally { }
}
}

Final Task: Deploy and Test the WebPart:

Right click on your project and select Deploy. now follow these steps to Add the web part to a web part page:

first Click the Edit button to put the page in edit mode.

then Click Insert =>Web Part => Custom => LINQWebPart Title => Add

The rendered web part will look like this.

Well Done !!!!!!!!!!!!

Exercise Summary:

In this walkthrough you built and deployed a Visual Web Part that uses LINQ to SharePoint to gather data from a SharePoint list.