Tuesday, February 22, 2011

How to Delete/Remove a Workflow from SharePoint Designer

When you create a workflow from SharePoint Designer, it is always attached to a single list of library. They just cannot be reused.

Removing a workflow is a two fold thing means if you just want to make the workflow unavailable to users or actually deleting it.

Removing a workflow through a browser does not actually delete it.

If a workflow is created through SharePoint Designer and removed through a browser, it just gets unavailable to users but all the source files used to compile a workflow remains stored in Workflow Document Library of the site.

To remove a workflow, follow these steps

1. Browser to the list which you want the workflow to be removed from.

2. On the list library menu bar, click Settings and then click List Settings

3. You will end up on page where you can customize your list. Under Permissions and Management, click Workflow Settings

Note: If you have a workflow associated with this list, you will see the list of workflows added and will be given an option to remove or add another workflow but if there is no workflow attached, you will be directed to "Add a workflow" page from where you can create Out of the Box workflows.

4. If you have workflows attached to your list, you are on Change Workflow Settings Page, click Remove a workflow hyperlink.

5. You will be provided with a list of workflows that are associated with the list. Click Remove radio button against the workflow that you want removed.

6. Click OK

Note: There is a difference between No New Instance and Remove. If you remove a workflow from a list by selecting Remove option, it will cancel all the running instances of the workflow. If you want to keep the current instance running, select No New Instance. This will help you complete the already instance and in the meantime will not allow any new action to be performed from the workflow that you want removed. Once all the instances are complete, you can come back to this screen and remove the workflow.

To delete a workflow, follow these steps

Deleting a workflow from SharePoint Designer is simple and as discussed earlier, deleting a workflow from SharePoint Designer will delete all the source files from the site and will stop all the workflow instances that are currently running on the list or library.

1. Open your site in SharePoint Designer.

2. From the Folder List Pane on the left of your SharePoint Designer, expand Workflows Folder

Note: If Folder List is not visible, click View>Folder List to make it visible.

3. Select the workflow that you want to delete and press Delete button on the keyboard. A confirmation message will be shown to you to confirm if you actually want to delete it, click Yes

Note: You can select multiple workflows by clicking workflows and holding CTRL key.

How to enable SharePoint Designer Workflows

We know that there are 3 types of workflows

  • Out of the box workflows

  • SharePoint Designer workflows

  • Custom workflows using Visual Studio and Workflow Foundation (WF)

By default SharePoint Designer workflows are enabled on a site in SharePoint 2007 (MOSS) but if you want to change the default settings (or if they are disabled) follow these steps

  1. Open Central Administration.

  2. Click Application Management tab.

  3. Under Workflow Management Section, click Workflow settings.

  4. Make sure you have selected the web application on which you want to perform the operation

  5. Click Yes or No for "Enable user-defined workflows for this site?" depending on your requirement.

Note: Users with Web Designer permissions can create and deploy workflows by using the Workflow Designer wizard in Office SharePoint Designer 2007.

Wednesday, February 16, 2011

CAML Query and Where Clause Examples

Here I will try to cover as many combinations of Where Clauses as I can.
I will also explain each of them a little. Keep visiting this post for more examples.

EQ: If you notice here, we don't have any "=" (equals to) sign. We have our equals to criteria wrapped inside EQ Tag.
<FieldRef Name='Title'/>
<Value Type='Text'>Hello World</Value>

AND: If we want to have data with a criteria which involves 2 or more column and a AND condition, this is how it looks in CAML. Notice we have all criteria nested inside AND
<FieldRef Name='Title'/>
<Value Type='Text'>Hello World</Value>
<FieldRef Name='Column2'/>
<Value Type='Text'>CAML</Value>

OR: OR schema is same as AND
<FieldRef Name='Title'/> <Value Type='Text'>Hello World</Value>
<FieldRef Name='Title'/><Value Type='Text'>Bye World</Value>

Greater Than: Any such operator can be used with or without AND/OR Clause
<FieldRef Name='Age'/><Value Type='Text'>18</Value>

Less Than: Any such operator can be used with or without AND/OR Clause
<lt><FieldRef Name='Salary'/><Value Type='Text'>$5000</Value></lt>

Order By: This clause is used independent of Where clause. If you do not define an order, default is ascending.
<FieldRef Name="Salary" Ascending="True"/>
<FieldRef Name="Age" Ascending="False"/>
<FieldRef Name="JoiningDate"/>
<!--[if !supportLineBreakNewLine]-->

CAML Query SPQuery Object and Where Clause

To fetch data/item (SPListItem ) from SharePoint List (SPList) we have to use CAML with SPQuery class.
We have already discussed CAML in our last post, now we will look at SPQuery class and Where Clause in a bit of detail in this post.

SPQuery Class is used to hold query, attributes, fields and other important properties.
To fetch data from list, we need to call GetItems function of SPList class.

Here is how it should be done.

SPList list = web.Lists["List Name"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Hello World</Value></Eq></Where>";

SPListItemCollection itemsCollection = list.GetItems(query);
GetItems of SPList class has 3 overloaded methods which takes SPQuery, SPList as arguments.
As SPListItemCollection is a collection which has SPListItem type items, we can access any item by either having a foreach loop or directly accessing an item by passing index.

CAML Query

CAML stands for Collaborative Application Markup Language. This language within the context of Microsoft SharePoint Services/Foundation to define query against list data.
To simplify things, as we have SQL to perform operations of SQL Server, we have CAML to perform operations on List.
Note: With SQL, we can perform a lot more complex operations which we cannot perform in CAML.

CAML is XML like structure with some of the schema elements defined below
WHERE <Where> </Where> Just like WHERE Clause in SQL but unlike SQL where we have WHERE mostly coming in last, here our query starts with a WHERE Clause.
AND <And> </And> Just like AND Clause in SQL but as explained above, its a XML like schema so every criteria has to be inside AND Tags.
OR <Or> <Or> Similar to OR Clause in SQL but XML like schema.
EQ <Eq> <Eq> Represents a "=" (Equals To)
NEQ <Neq> <Neq> Represents a "!=" (Not Equals To)
GT <gt > <gt > Represents a ">" (Greater Than)
LT <lt > <lt > Represents a "<" (Less Than)
FieldRef <FieldRef> <FieldRef> Can be considered as a column in SQL Server but with different attributes

A standard CAML QUERY
<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Hello World</Value></Eq></Where>
More on CAML in next post

Tuesday, February 15, 2011

Programmatically add records in a List in SharePoint

using Microsoft.SharePoint;

using (SPSite site = new SPSite("SITE URL"))
using (SPWeb web = site.OpenWeb())
SPList list = web.Lists["LIST NAME"];
web.AllowUnsafeUpdates = true;
SPListItem item = list.Items.Add();
item["TITLE"] = "Hello World";
web.AllowUnsafeUpdates = false;

Now for some explanation :

using Microsoft.SharePoint; This namespace is added at the start of the .cs file to use available classes. We have to first add a reference to Microsoft.SharePoint.dll file by right clicking your solutions/project/web application and selecting Add Reference option.
If you are working on SharePoint 2007 (MOSS) browse to 12 hive and then to ISAPI folder and select Microsoft.SharePoint.dll (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI). If you are working on SharePoint 2010, the folder is 14.

After adding reference importing the required namespace, we have a Security Context

using (SPSite site = new SPSite("SITE URL"))
using (SPWeb web = site.OpenWeb())
/do something there

If you are logged in as a user who does not have rights to perform any insert operation, we elevate privileges. This feature is provided in Windows SharePoint Services 3.0 to perform actions in code programmatically using an increased level of privilege.

using scope is introduced so that as soon as SPSite and SPWeb objects gets out of context, they are disposed of automatically

SPSite site = new SPSite("SITE URL")We first open the site in which we have the list on which we want to perform insert operation.

SPWeb web = site.OpenWeb()We open the web in which we have the list of which we want to perform insert operation.
SPList list = web.Lists["LIST NAME"];Now we create object of SPList Class with the list in which we have to perform insert operation. "LIST NAME" is the name of the list which needs to be accessed from your code.

web.AllowUnsafeUpdates = true; We get security validation errors if we do not set our web for AllowUnSafeUpdates.

SPListItem item = list.Items.Add();SPList class is collection of list items. Just like any other collection, say DataTable for instance, whenever we want to add a new item to collection, we call the Add method to return SPListItem object. Simply speaking, this object represents a row in the SPList list.

item["TITLE"] = "Hello World"; As we discussed above that SPListItem instance represents a row in your list, now you can access columns/fields and set valid values in them.

web.AllowUnsafeUpdates = false;After setting the values, we update our item, we finally update our list which inserts data in the list. We initially set web.AllowUnsafeUpdates to true to avoid errors. Now we are done with our insert operation, its better to set it to false again.
The code will automatically move to the next statement which is closing braces of our using block which disposes our SPWeb and SPSite objects.