Monday, April 30, 2012

Working with Chart web part in SharePoint 2010


One of the lacking feature in MOSS 2007 was not having functionality to draw chart based on various sources even not with list.

Now with SharePoint 2010, we can draw chart not only from lists but also from various sources like SQL server or excel for example.

In this post, I am going to demonstrate you a way to draw chart using list by using SharePoint Chart web part.

I have a list in my site with following data. Data is only shown for an example. Real scenario can be different based on business need.









Find and add chart web part to a page.



The moment you add chart web part, you get something like this



Click on data and appearances. It gives you two options, connect with the data and customize your chart.



Click on connect chart to data. You get an option to get the data from list, BDC, excel or another web part. Select list.



Advantage is you not only can select list from the same site but from the entire site collection.



You get a screen showing data from the list. You can also set the filter option as mentioned in a screen shot. It allows you to return only limited number of data from the list if you wish to. You can also preview it. I am leaving it empty.



Now you are on a screen where you can actually bind the data with the chart web part. Here we can select type of chart, columns for x and y axes, labels for the chart, Grouping option etc.




Click on finish. Bingo!! We now have a chart on our page.



But still it does not give proper picture. So let us go ahead and now click on advanced properties.

You can set various properties here. You can change back ground color, gradient style, hatch style, skin style, change the palette.

You can check out different options from left hand side which also allows you to show legends, 2D or 3D chart.



You can also show labels of data.



If you would like to have connection for the chart web part, you can also use content menu and then configuring data section.



Now if you go back to data and appearances and click on customize the chart, you get an option to select a chart type from range of different chart types.



And then you can follow the same procedure to configure the chart the way you want.
The best part is the chart web part is dynamic, means any changes to the data or any addition to the data gets reflected automatically

Let’s change our existing list data and add one more list item.

I changed February data from 1670 to 2500 and added one more list item for September month.





I hope you like this post. Keep exploring chart web part which is definitely a welcome addition to SharePoint 2010.

Monday, April 23, 2012

Implement SharePoint 2010 Site event receivers


In SharePoint 2010 we now have a new type of receiver which is related to site. We now can track when a web is adding, provisioned, moving, moved, deleting and deleted.

All receivers that end with ing are synchronous and those which ends with ed are asynchronous.

So let us go ahead and explore some of the receiver types and try to understand how it works.

Open up visual studio 2010 and select SharePoint 2010 and select event receivers in the template type.



Connect with the site and select farm solution.



Select Web receivers from the list. Let us select these four options and explore them. Rest you can check on your own and let us know if you face any issue.



You then will be presented with four receivers.



Go ahead and add following code in each of them. Now build and deploy the solution. And now try creating site, and then delete the site.

public override void WebDeleting(SPWebEventProperties properties)
       {
           //This code actually triggers for sub sites that is being deleted. Hence we have taken reference of parent web to write the entry.

           SPList lstTracking = properties.Web.ParentWeb.Lists["Track Different Events"];

           properties.Web.ParentWeb.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Deleting the Web";
          
           item.Update();

           properties.Web.ParentWeb.AllowUnsafeUpdates = false;
         
       }

       ///

       /// A site is being provisioned.
       ///

       public override void WebAdding(SPWebEventProperties properties)          
       {
          
           //This handler will trigger at the time of web being added as a sub site. Track Different Events list is in the parent site.

           SPList lstTracking = properties.Web.Lists["Track Different Events"];

           properties.Web.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Adding the Web";

           item.Update();

           properties.Web.AllowUnsafeUpdates = false;

          
       }

       ///

       /// A site was deleted.
       ///

       public override void WebDeleted(SPWebEventProperties properties)
       {
           //This code actually triggers for sub sites that is deleted. Hence we have taken reference of parent web to write the entry by
           //instantiating SPSite. properties.Web returns null as web has been deleted by this time.

           SPSite site = new SPSite("");

           SPWeb web = site.AllWebs["ECMA"];

           SPList lstTracking = web.Lists["Track Different Events"];

           web.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Web has been deleted";

           item.Update();

           web.AllowUnsafeUpdates = false;
       }

       ///

       /// A site was provisioned.
       ///

       public override void WebProvisioned(SPWebEventProperties properties)
       {
           //This code actually triggers for sub sites that is created. Hence we have taken reference of parent web to write the entry.          

           SPList lstTracking = properties.Web.ParentWeb.Lists["Track Different Events"];

           properties.Web.ParentWeb.AllowUnsafeUpdates = true;

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Web has been provisioned";

           item.Update();

           properties.Web.ParentWeb.AllowUnsafeUpdates = false;
       }

We’ve created a site. So now we can see two entries in the list.




Now delete the sub site and we get this.






Thursday, April 19, 2012

Result is out

It seems that IE will have tough time in future


Wednesday, April 18, 2012

Create Folder in library using ECMA client object model

Here is a simple way to create folder inside library using ECMA client object model



<script type="text/javascript">
var clientcontext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(AddFolder, “sp.js”);
function AddFolder()
{
clientcontext = new SP.ClientContext.get_current();
web = clientcontext.get_web();
this.list = web.get_lists().getByTitle("LibraryName");
var listItemCreationInfo = new SP.ListItemCreationInformation();
listItemCreationInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
listItemCreationInfo.set_leafName('NameofTheFolder');
var newItem = list.addItem(listItemCreationInfo);
newItem.update();
clientcontext.load(web);
clientcontext.load(list);
clientcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));
}
function ExecuteOnSuccess(sender, args) {
SP.UI.Notify.addNotification('Folder has been successfully created', false);
}
function ExecuteOnFailure(sender, args) {
alert("Some error");
}
</script>

Monday, April 16, 2012

Exception handling in client object model


We are familiar with try, catch and finally. It has been around for many years. We cannot ignore that at all as it plays a vital role in development.

Same is the case when we write a client object model code for SharePoint 2010.

ExceptionHandlingScope is a new class that has been introduced in SharePoint 2010 which allows us to perform the same action.

Let’s say you are working with managed client object model.

Here is a way to get a context of ExceptionHandlingScope and to work with it.

ClientContext ccontext = new ClientContext("site collection path");           

ExceptionHandlingScope exceptionscope = new ExceptionHandlingScope(ccontext);           

using (exceptionscope.StartScope())
            {
                using (exceptionscope.StartTry())
                {
                               // Code Block
                }
 
                using (exceptionscope.StartCatch())
                {                    
                     //Code Block which handles the exception
                }
 
                using (exceptionscope.StartFinally())
                {                    
                    //Code Block that always executes
                }
            }

Let’s say you are working in ECMA client object model and would like to handle exception

function Example()
{
this.clientcontext = new SP.ClientContext.get_current();

var clientScope = new SP.ExceptionHandlingScope(this.clientcontext);

var startScope = clientScope.startScope();

var tryScope = clientScope.startTry();

// Code Block


tryScope.dispose();

var catchScope = clientScope.startCatch();

 //Code Block which handles the exception

catchScope.dispose();

var finallyScope = clientScope.startFinally();

//Code Block that always executes

finallyScope.dispose();

startScope.dispose();

}

I hope this will help developing smoother and robust client object model application.

Friday, April 13, 2012

Custom Task form in workflow


In workflow we many times have a requirement to create custom task form four our business logic. Yes you must be telling that it was possible through creating custom content type which inherits from task content type and then associate that with workflow in an workflow xml file.

All true, but with SharePoint 2010 designer now we also can customize task form for any of the workflow.

We are going to see how we can customize it and also we are going to see what new actions are available for that process.

I have one list called Orders and we are going to associate a workflow and then in that we are going to have custom task form. So open SPD 2010, connect with the site and then go to workflow.

Create a workflow and attach it with a list called “Orders”. In your case it may be a different list than me.

Now we are going to add an action called start approval process.


Click on the first parameter in the action that we need to configure.



This allows us to configure the entire task handling process the way we want. Here we can handle when the task end, overall behavior of task process, individual should task item process etc.



As you can see you can also configure reassignments, change requests are allowed in the task or not.

If you look at the right panel in task editor, you will find fields. I have already added three fields to it just for a demonstration purpose. Do not go on the logic. But it will serve the purpose of customizing the task form.

Go ahead and add some fields to it. These fields will appear on the task form. You can also choose the existing field.



If you look at change the completion condition, change behavior of individual task and change overall task process closely, you will find a lot of conditions and actions inside them. This is the pre-built logic that follows approval process.

If you wish to make changes to any of these, feel free to do that and see how your workflow goes according to the change that you make.

I am going to add some logic to each task. We can add logic when task it created, expires, Pending, deleted or even completes.



You can also change the overall task process. For example, if in your requirement you have five approvers and if three out of five approves then you do not want to waste your time with two more approvers to approve or reject. You simply want to approve the item as majority of them have approved it. In this scenario, you can use overall task process.

If you go back to the workflow settings to edit the workflow, (one level above task editing page) where we had added start approval process on this item with approver.

Click on approver.



Here you can select any individual, active directory DL or SharePoint group or even field from the list or library.

You can also define to start the process as a sequence or in a parallel.




















Let’s publish the workflow and then test the workflow. Make sure that you have selected start workflow when new item is added option from workflow settings page.

I’ve added an item to orders list and workflow has now started as you can see the status in in progress.



If I now open the task allocated to me, I would see three more fields in the task form.



Once you are done with all the task items, you will see outcome that is again governed by the workflow logic.



You can also customize the way task form looks like. Open the Approval Task Form.



You should have the InfoPath to perform the modification in a form.

Save it and publish it. Advantage it you are customizing task form only for your custom workflow and not globally.

Now then we have modified the workflow, let’s go ahead and add one more item in orders list to see this change.




If you would like to make any change in your conditions inside workflow, you can also get these additional fields that you’ve added on the form. We have external participant and reason in the list.



I hope you have liked this post. Do try different actions and conditions. Try out customizing task process, outcome. Let us know if you face any difficulty or query and I would be more than happy to answer them if I can. J

Wednesday, April 11, 2012

Add metadata to folder content type


When we want to add folder to document library, it is a very basic step of creating folder. Now what if I say create a folder with metadata like the way we have for documents. Now this is interesting. Isn’t it?

There is no way by default to have metadata for folders. We create columns for document library that actually becomes metadata for documents and not for the folders.

So let us go ahead and create a custom content type. We cannot change default folder content type as it is sealed.

Go to site actions, site settings, site content types.



Click on create



Give a name advance folder and keep the settings like shown in figure below.



Click on ok and then click on add from new site column when you are on content type settings page.



And then add a column called department



Now go back to library, set allow management of content type to true from library settings , advanced settings.



Click on add from existing site content types



Select custom content type, select advance folder and add it



Now under New Document, you can find advance folder.



Once you click on it, dialog opens to create folder with a column that we had specified earlier.



Once you add that then you should be able to view the folder in library and by editing its property you get to see the department column in that.

Hope this helps.



Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com