Wednesday, June 13, 2012

Understanding Data Form Web Part in SharePoint 2010/ MOSS 2007 – Part 3


If you have not gone through Part -1 and Part -2 of this series, I would recommend you reading it first and then continue reading from here.

Let’s go ahead and add form actions to data form web part.

Open the page that we had left opened in our previous post also open the list in browser. We are going to add email field to the list. I have entered some dummy email addresses to the list items.



Open SharePoint Designer page and add email field to the data form web part.



Now keeping cursor in email field, right click and then select insert column to right.



Now keeping cursor in that column, go to insert tab and select SharePoint controls, form action control button.



The moment you select the button, it asks you what you want to perform when button is clicked. We are going to select custom action.


So what happens is it generates a workflow that is not associated with the list, nor associated with site or not even with list item. It is independent workflow that can be triggered by the button.

It generates a workflow named form actions workflow 1.

Locate the property of button and change the text of button to send email. 

Now we need to make one more change because we are going to send an email to Email field mentioned in that specific list item, hence we first need to convert that field into a label so that we can make the reference of that field in the workflow.

So select the email field, right click that field and select format item as a label and then save the field.



Now locate the Form actions workflow 1 under workflows tab and open it to modify.



Select action - send an email.



And select these users for email and then select workflow lookup for user



Once you are done with this and if you have email server properly set up, then you should be able to send an email by clicking on that button which will send email to Email field mentioned in that list item.



Monday, June 11, 2012

Understanding Data Form Web Part in SharePoint 2010/ MOSS 2007 – Part 2


If you have not gone through part -1 of this series, I would recommend you reading it first and then continue reading from here.

If you open a page which we designed in previous part in browser and edit the page and then edit the web part, you will see XSL editor and parameters editor.


If you observe closely parameter binding, you will also find our color parameter set there. This way you can also enter multiple parameters binding here as well.



Now let’s get back to designer and select the data form web part. We are going to apply conditional formatting now.

Click on Table tab, then select and then row. This will select the row in data form web part.



Keeping the row selected, select options again and then conditional formatting and then formate row.



Select if color equals to red, and then click set style



Set background to red



Repeat this for remaining colors. Once you set them for different colors, you should have something like this in designer.



Save it and open it in the browser. You’ll see different color rows based on the color field value.



So now if you go and open the web part in edit mode, and see the XSL you will see following written.



Now go to table, select column and then make it bold lettered.



And you can see quantity turning into bold letters



Now we are going to see editing functionality and multi entry on data form web part. For a demo, I have removed the existing web part on a page.

Now find Data View option in ribbon and then manage data sources.



And select stock items colors list. You can select the list you want.

Select the web part and click on inline editing option on the ribbon.



Save it and view it in the browser. You should be able to edit the item without opening it in separate edit page.



You can also select what operation should you allow.



Now coming back to designer, select any empty div on the page and then select title, quantity and color and then click on insert field as multi item form.



And save it and open it to view in browser.



In next post, we are going to see how to use custom actions with the list or library using Designer. Read Part-3 for further reading.

Thursday, June 7, 2012

Understanding Data Form Web Part in SharePoint 2010/ MOSS 2007 – Part 1


We are going to start a new series of data form web part. Data Form web part is one of the most important web part in SharePoint.

 First to start with, let me clear the difference between data form web part and data view web part because this is the most common question arises when it comes to these two web parts.

Data View web part was introduced in WSS 2.0 and Data Form web part was introduced in WSS 3.0. So obviously data form web part is advanced than data view web part. However it depends on what you want to perform.

Data view web part as name suggest only be used to view the data. However data form web part writes back the data to the source as well.

Both web parts can only be created from SharePoint Designer and not from the browser. Both still uses XSLT to transform the data into the representation form. But data form web part uses SPDataSource control to connect with source and fetch the data.

We are going to see what data form web can do.

First it can fetch data not only from SharePoint but also from SQL server, web service, and XML file.

It can also take parameters to display data in it. Parameters can come from query string, it can also be cookie, and it can also be form fields.

So connect with your site via SharePoint Designer. Open a page where you want to add data form web part.

I have one list in site.



Now keep the cursor inside place holder and click on data view and then click on Stock Item colors list.



Once you add it, you get something like this.



At this point of time, just save the file and open the page in browser.



Now go back to designer and select three fields by holding ctrl key and then click on Insert selected field drop down and select multiple items view and you’ll get this.



Again save and check it in browser.



Now let’s go ahead and select the data form web part. Look at the ribbon and you see you can configure different options like group, sorting, filtering and parameters.

Let’s click on sorting option.



Sort by color in ascending order



You can also click on show group header option and you get something like this



Save it and view in the browser.

Now we are going to add parameters. So go back to designer and click on parameters option. By default you will see these two parameters.



Go ahead and click on New Parameter and select type as query string. As you can see you have difference option to define the source of this parameter.



Now we have parameter so we now need to configure the filter based on this parameter.
Select data form web part and then click on filter. Then select color as field and set it like this.



Once set you’ll get this in designer. It does not show any data because there are no colors defined now.



Click on sample data in design tab.



Save the page and open the page in browser.

Initially you will see no data. Pass the parameter and value to see data in the web part.




Continue reading Part -2 for further understanding.

Tuesday, June 5, 2012

Developer Dash board in SharePoint 2010 – Part 2


If you have not gone through Part-1 of this series, I would recommend you reading Part-1 and then continue reading from here on.

Okay, so now we have turned our developer dashboard to OnDemand. So let’s open up our site.

And observe one icon on the top right.



This is the icon for developer dashboard. When you click on it you can see at the bottom of the page which displayed the rendering time taken by each method and time taken by database calls. What all methods have been called, Service queries, web part events etc.



Click on the icon again to hide it.

When you move to any other page, dashboard again goes off. You again need to click on icon to get it back.

If you always want it to be there then you have to set in settings of developer dashboard as on rather than ondemand.

Let’s say we have developed our custom web part and we have placed that web part on a page. So navigate to a page where you have kept the web part and click on developer dashboard.



And here you get in developer dashboard

As you can see it was visual web part and we can see how much time taken by that visual web part to load.

This gives us little information. But what If you actually want to know how much time code written on button click takes. Generally if you want to know time taken to perform action or load some events and we want to include that time as a part of developer dashboard, then we need to use SPMonitoredScore class.

Let’s dive into SPMonitoredScopre class.

Overall idea is we need to wrap up our code inside SPmonitored scope and the way we should do it is ;

using (new SPMonitoredScope(“Name of the scope”))
{
   doSomeWork();
}

When we do this, the code written in doSomeWork method captured by the developer dashboard and you can track down more details on code tracing.

Let’s add some examples to a web part that I showed you earlier on a page.
I have added a code in one of the button click event.



Now redeploy the solution and let’s see what happens when we click on that button.
If you now observe you get to see on left pane your own custom scope.



This also adds an entry to Log file on the server. If you open the latest log file and search for scope name, you get this



In this way, you can trace time taken by each method. Do not use this class everywhere. Use it with caution as if you unexpectedly use this class to trace a long running method then it may add few extra burden on page and increase its size for adding to the developer dashboard.

We can also add developer dashboard icon when you are developing your custom master page. 

This is very important to have. All we need is just use developer dashboard SharePoint control in master page. Just to demonstrate I have added in web part.



And here is what you get



Couple of important notes that you should know is that

  1)     SPMonitoredScope class can only be used if your custom development is going to be deployed as farm solution and not as sandbox solution. Sandbox solution runs in different process than farm solution as it is not captured in w3wp process.

2)    Use another class called SPDiagnosticsService to log tracing and performance timing for custom solutions in LOG folder of server that do not run under HTTP like SharePoint timer service.

I hope this must have given some basic idea as how to use developer dash board and its use in SharePoint.

Friday, June 1, 2012

Developer Dash board in SharePoint 2010 – Part 1


Developer dashboard is a new feature introduced in SharePoint 2010. 

Developer dashboard as a name suggests helps developer to find about their code’s execution time and performance tuning. It shows how much time each database query takes and how much time web part takes to load. 

Where is the code which takes longer time to execute and delaying the page load?

Developer can easily introduce extra SPSite or SPWeb in code which can be captured from here.

It is almost like tracing in ASP.net where we trace request.

Developer dash board id by default turned off. You cannot find where that is. We need to turn it on.

There are three ways you can turn on developer dash board, through obsolete stsadm command, through new powershell command or through object model.

We are going to see each of them.

Before turning developer dash board on, we need to know few things. There are three switches for developer dash board. You can turn it on then it will always be available.

You can turn it off hence it won’t be available and you can set it to OnDemand which is the most proper setting that suits the developer. 

When we want to see we can click on icon and when we do not want to see we can once again click on icon to turn it off.

1)    Through STSADM

STSADM –o setproperty –pn developer-dashboard –pv OnDemand

Just change On or Off mode at the end based on requirement

2)    Through PowerShell

$DDboard = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$ DDboard.DisplayLevel = 'OnDemand';
$ DDboard.RequiredPermissions = 'EmptyMask';
$ DDboard.TraceEnabled = $true;
$ DDboard.Update();

    Just change On or Off mode at the end based on requirement for DisplayLevel property.
Now here is an interesting stuff. If you see required permission property, we have set as EmptyMask that means all users will be able to access developer dash board.

We can set any SPBasePermissions mask here that will restrict only those users to view this dashboard setting.

3)    Through object model

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
    
SPWebService DDboard = SPWebService.ContentService;
DDboard.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.On;
DDboard.DeveloperDashboardSettings.Update();
 
Now here is the interesting catch. I have created windows application to turn this developer dashboard to OnDemand mode. But if I write above code I was getting exception.
 

 
Well the problem is in Visual Studio 2010. Now you must be wondering what that problem can be. Well, answer is go to the properties of the project and change platform target to x64.
 

 
And there you go. You should be good to go then. Handy tip. Itsn’t it?
 
Change the enumeration to OnDemand or Off based on your requirement.

Keep reading Part -2 of this series.

Wednesday, May 30, 2012

Property bags in SharePoint


Property bag in SharePoint exist in MOSS version and even continued to be a part of 2010 version. Property bag is nothing but a key value pair stored as hash table that we can retrieve in object model.

We can add, edit or remove properties from property bag from SharePoint Designer or through object model.

So let’s go ahead and check that out in Designer. Connect with the site.

Check out site options and click on it.

Here you can see parameters; these are the properties for this site. Here we can add, edit and remove. Let’s go ahead and add three properties and values.

Click on Add give name and value.



In the same way create two more properties.

Before we dive into coding part, I would like to bring in some more details.

We can define properties for several levels in SharePoint. At highest level we can have it for SPFarm, then SPWebApplication, then for SPSite, for SPWeb and for SPList.

Let’s us start by adding and retrieving properties that we just defined at web level.

  protected void btnPropertyBag_Click(Object sender, EventArgs e)
        {
            SPWeb objWeb = SPContext.Current.Web;
            objWeb.Properties.Add("CustomKey4AddedFromCode", "CustomValue4AddedFromCode");  
            objWeb.Properties.Update();  

            string strProp =  objWeb.AllProperties["CustomKey1"].ToString();
            strProp = objWeb.AllProperties["CustomKey2"].ToString();
            strProp = objWeb.AllProperties["CustomKey3"].ToString();
           
        }  



Sometime you can have Properties and not AllProperties like for SPlist, , SP web application and SP farm.

You can also remove property that we’ve added.

  objWeb.Properties.Remove("CustomKey4AddedFromCode");


If you want to check if the key exist, use containskey

if(objWeb.Properties.ContainsKey(""))


If you want to add it to the list or library, then you won’t find properties collection directly. You need to take rootfolder object and store it there. Like shown below.

SPWeb objWeb = SPContext.Current.Web;

SPList lstCalendar = objWeb.Lists.TryGetList("Calendar");

lstCalendar.RootFolder.Properties.Add("CustomCalendarKey", "CustomCalendarValue");

For SPSite there is no as such property bag available, but if you want to store, then use rootweb for SPSite and then assign properties. Like shown below.

SPWeb objWeb = SPContext.Current.Web;

SPSite objSite = objWeb.Site;

objSite.RootWeb.Properties.Add("SiteLevelKey", "SiteLevelValue");

For Web Application you can use this

SPWeb objWeb = SPContext.Current.Web;

             Microsoft.SharePoint.Administration.SPWebApplication objWebApp = objWeb.Site.WebApplication;

             objWebApp.Properties.Add("WebAppLevelKey", "WebAppLevelValue");

 Hope this will help to understand property bag.



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