Wednesday, March 10, 2010

Creating custom activity in Workflow Part – 3

Hi All,

In last two articles, we saw how to create basic activity and how to work with validations of properties.

If you haven't gone through previous parts, then i recommend you reading Creating custom activity in Workflow Part – 1 and Creating custom activity in Workflow Part – 2 post before continue reading here.

In this post, I am going to talk on Tool box item with respect to custom activity. When we want to initialize properties or want to perform any action when activity is being added (dropped) on visual designer, we can use toolbox item for this.

To achieve this functionality we have to implement two steps.

1) Create Toolboxitem class for custom activity deriving from ActivityToolboxItem class.

2) Add ToolBoxItem attribute to the custom activity class.

These two steps shown above are basic steps that you will find in all parts of this series. Because we have to perform above same steps (just inheritance is a difference) to achieve stuff for custom activity.

So let us go ahead and assign default birth date to our birth date property (I know it does not sounds anything practical, However as I said if we use simple example then it makes life easier).

One more important point to mention here is that we have to mark our class serializable and also we have to call deserialze in the constructor of the class. Reasons for this, well it is a topic of its own. I will explain it later. As of now, we will implement them to demonstrate our example and anyhow it’s merely two-three lines to write.

Go ahead and add class to our custom activity project. Name it CustomActivityToolBoxItem.



Then, go to the custom activity class and add System.ComponentModel which will help you to add attribute to the class.

Add the newly build assembly to the toolbox and drag and drop on visual surface and see the magic. You will see default birth date property being set by our code.



See you soon with part 4.
Read More…

Monday, March 8, 2010

Creating custom activity in Workflow Part – 2

Hi All,

In my first post Creating custom activity in Workflow Part – 1 , we discussed about creating basic custom activity. Now as promised in this post, we will move a bit and hit the second topic of validation with custom activity.

When we gave input of birth date in first activity, we didn’t create any validations for it. This post will tell us how to create validation that will help us to validate the input user is providing to the parameters. So we will force user to provide correct birth date.

Bottom line is we can use the validation techniques in custom activity for each parameter that we define for our custom activity.

Adding validators consist of two steps.

1) We have to create a class which inherits from ActivityValidator Class.
2) Add ActivityValidator attribute to the custom activity class.


We have to override Validate method and we will return ValidationErrorCollection which hold all validation errors for birth date.

For validation logic, we will simply try to validate date against regular expression and if not matched, then we will throw the Error from our validator class.

Keep in mind that we are using the same custom activity that we’ve created in part 1. Hence if you haven’t gone through part 1, I recommend read it first and then carry on with this example.

So go ahead and add one class to the Workflow Custom activity project and name it CustomActivityValidator and inherit with ActivityValidator.



And then go to Custom Activity class and just add one attribute there on top of the class line and also add System.Workflow.ComponentModel.Compiler using statement.



Now just build the custom activity project again, and remove the previous custom activity on our console project and drag new (modified just now) activity to console application, and try to enter invalid date in property and valid property, and see. First we will enter valid date and then invalid date.





Read Creating custom activity in Workflow Part – 3 for furher reading.
Read More…

Friday, March 5, 2010

Creating custom activity in Workflow Part – 1

Hi All,

After long time, I am back to the workflow and really I have spent many months without writing anything related to the workflow.

See Creating custom activity in Workflow Part – 2 for further reading after reading this post.

This post will talk about creating custom activity in workflow. Is this the right time to talk about this topic, when you have bunch of articles already written on the net and books. Well, I think almost all of them starts with complex example of creating custom activity. Even in some books, they start with complex example, never written simple examples.

So I thought of giving a change to myself to write down very basic stuff and example about custom activity.

Custom activity in workflow is just like the concept of creating custom control for .Net applications. It can be user controls or a custom control. In the same way, in workflow world it can be single activity (Basic activity) or composite activity. Creating single activity is somewhat easier than creating composite activity. For example, Delay is basic activity and While is a composite activity.

Other one important point to remember is basic activity derives from System.Workflow.ComponentModel.Activity and composite activity is derived from System.Workflow.ComponentModel.CompositeActivity.

An activity is made up of four components. All are not required, only definition is required for an activity. However definitely we want to execute something in that, hence executor is also important.

1) Definition: This is the definition of an activity where we define properties, events etc.
2) Validator : This is for validating input properties value.
3) Executor: Execute the actual code in the activity.
4) Toolbox: This will help us to assign default values to properties at the time of dragging on Visual Studio designer surface.
5) Designer: This is about makeup of the activity. (You will say, yes definitely a makeup :) )

We will see each one of them in different parts. We will start with part 1 in this post and continue to explore each option in series.

So let us go ahead and start writing our basic code to create custom activity.

First open visual studio and choose workflow under C# and then choose custom activity. Add a class and perform the following steps. As we are creating basic activity, we will inherit from Activity class.

Make sure that your class is marked public; otherwise you will not be able to place activity in toolbox itself.

We have to create one dependency property and we are going to create BirthDate property here and our basic activity will return the age of a person.

Then we will override the Execute method and execute my logic and then we will return our ActivityExecutionStatus, that means if it succeeds, we will return closed and if not, we have to return Faulting.






Now go ahead and add one more sequential console workflow project to existing solution, and click on choose items on toolbox, browse to the DLL of just created custom activity and there you go, you will have it in the toolbox, just drag and drop it on the surface. Enter the BirthDate in BirthDate property, we’ve created.



Run it, Debug it, try it and see for yourself.



I will be back very soon with part 2.
Read More…

Wednesday, March 3, 2010

Finally the result is out

Hi All,

Again thank you for your support in giving opinions about polling questions.

We asked about new ribbon control of SharePoint 2010 and almost people have welcomed this change.

Question was :

How do you see new ribbon interface in SharePoint 2010?

Options were :

1) Welcome change in SharePoint 2010. Making consistent with office product.
2) Have they really changed anything? It is just a way of presenting options differenty.

Here is a result.

Read More…

Tuesday, March 2, 2010

Version difference in Site Settings and Central administration

Hi All,

One day somebody asked me, what is the version number of your SharePoint? And I did not have answer because I was not too sure where to look for. However it came in my mind that we can see version number on site settings page. Hence I opened my site settings page and gave answer to a person who asked me last evening.

So what is the big deal here in version number? Nothing new, you just have the version number of your SharePoint on site settings page.



Wait a minute. Have you ever observed version number as part of Farm Information in central administration. See below image for it.



In my machine, version number under site settings and version number under farm information are same. However, if you see a different version number in any of them, then there could be some reason for this. May be following reason is responsible for this version difference.

1) You may have applied some service packs related to MOSS / WSS 3.0 and you forgot to run SharePoint technology and configuration wizard. You must complete that wizard after applying service pack and that too successfully.

So bottom line is whenever you apply any service pack or when ever SharePoint asks you to run the wizard, just run it successfully and you should have same version number under both above mentioned screen.
Read More…

Thursday, February 25, 2010

Taking list item back up with attachments

Hi All,

Sometimes we require taking back up of list items along with attachments. Definitely there is an option to export to excel, however that doesn’t copy attachment with it and hence it is a drawback only if you want attachment with list item as well, else export to excel is really a cool functionality.

Coming back to the point, so what do we do to get the attachment as well? Well, the answer to this question lies in the same menu and that option is Open with Access.

So go ahead with your list items that you want to back up. Open the Actions menu and click on open with Access.




Specify a new location for your database and also chose whether you want to be connected with SharePoint list so that any changes can be reflected here and also vice versa, or just you want to have a copy of it which is export the data.



That’s it. You have just taken a back up of your list items with attachments.
Read More…

Tuesday, February 23, 2010

Set audience targeting programmatically

Hi All,

Today I am going to explain you about audience targeting. However I am going to share in terms of programming aspects. Because we all know that we can create our audience in central administration and then simple apply them on list items or document library items or on web parts on pages.

However we will see it this time how to do the same using code.

I assume that you have already created audience named “Sales” and “Finance”. I am not going in deep discussion explaining you each and every class and methods used in it. You may query me your doubts and I will be keen to reply you back.

using (SPWeb objsite = (SPWeb)properties.Feature.Parent)
{
using (SPLimitedWebPartManager wpm
= objsite.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
{
AudienceManager am = new AudienceManager(ServerContext.Current);

wpm.WebParts[0].AuthorizationFilter
= string.Format("{0};;;;", am.GetAudience("Sales").AudienceID);

wpm.SaveChanges(wpm.WebParts[0]);
}
}


We first take our web context and then take out the default.aspx web parts. We use webpartmanager to deal with them. Then take out Audience manager class with the context, and then interesting part comes. Authorizationfilter takes argument in such way that we have to pass ;;;; in it after getting the audience target ID. Get the webpart that you want and pass its Title let’s say in wpm.WebParts[0].AuthorizationFilter line. I’ve used 0 just to get the first webpart and show you the demo.

Here we want to target sales people, so we have passed Sales and get the ID of it and finally call up the savechanges method of webpartmanager object.

As simple as that.

Hummm…What about Finance people. Well there you go, you can set multiple audience in one go.

Just change the line to.

wpm.WebParts[0].AuthorizationFilter
= string.Format("{0},{1};;;;", am.GetAudience("Sales").AudienceID, am.GetAudience("Finance").AudienceID);

Your job is done.
Read More…

Monday, February 22, 2010

System.IO.FileNotFound exception in SharePoint 2010

Hi All,

Just came to know about very basic stuff in SharePoint 2010. Let’s say you are creating one simple console application in VS 2010 that just displays your site name.

using System;
using Microsoft.SharePoint;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (SPSite objSiteColl = new SPSite("http://myurl"))
{
Console.WriteLine("Title of the site is : {0}", objSiteColl.RootWeb.Title);
}

Console.ReadLine();
}
}
}



And you run the code, you get System.IO.FileNotFound exception saying that web application is not found, verify that you have typed the correct URL. So what do we do now? We go back and check the URL,hum…URL is perfect, nothing wrong about this. So small program and still not able to run it?

Well, answer lies in VS 2010 because by default build property is set to x86 and SharePoint does not work on x86 platform. As we all know that now MS wants everyone to move on 64 bit PCs. (Another money eating machine), so go to project properties, go to build tab and change the platform target to x64 and try now.

There you go; I think your error might have gone now. What say?
Read More…

Saturday, February 20, 2010

Backup and Restore SharePoint site collection from one Farm to another Farm using STSADM commands

Scenario: I have server called “ABC” where there is site collection called http://abc.com and I want to backup this site collection and restore it to new SharePoint farm “XYZ” http://xyz.com
Source Server:
Server Name: ABC
Site Collection URL: http://abc.com
Site Owner: ABC/administrator
Take backup using following command:
STSADM.EXE –o backup –URL http://abc.com -filename “backup_abc.bak” –overwrite
Help URL for backup: http://technet.microsoft.com/en-us/library/cc263441.aspx
Now to restore it to destination SharePoint farm
Destination Server:
Server Name: XYZ
Site Collection URL: http://xyz.com
Site Owner: XYZ/administrator
Create blank Web Application from Central Administration
Now restore using STSADM command
STSADM.EXE –o restore –URL http://xyz.com -filename “backup_abc.bak” –restoremethod New –newdatabaseserver “DATABASE_SERVER_NAME”
Change the site collection owner of this newly restored site
More details for restore is here
http://technet.microsoft.com/en-us/library/cc262087.aspx
Hope this will help you, let us know your comments on it

SharePointKings Team.
Read More…

Friday, February 19, 2010

SharePoint Solution (.wsp) smartsolutionupgrade STSADM

Hi All,
Yesterday,I came across one nice feature from "Trentacular SharePoint 2007 Features" CodePlex, here is a link http://trentacularfeatures.codeplex.com/

Actually, I have created custom site definition and it has some custom site pages, I have created SharePoint Solution package (.wsp) file to deploy it.
Now I have created 10 sites from this site definition after few days I need to change in few SharePoint features and custom site pages. Now I wanted to upgrade this changes on already existing 10 sites.
When I upgrade command using stsadm -o upgreadesolutions it upgrade the solution but to get effect of that upgraded solution to existing 10 sites i need to deactivate features of site pages and activate it. that thing i wanted to automate. I do not want to loop through each site and deactivate feature and activate it.

I got above nice solution that does smart upgrade it make inventory of features installed on your .wsp file and which site it is activated. It will upgrade solutions
deactivate features and activate it again and your task is done :)

Really really nice features we should use it for smartupgradation

Let us know your comments on it.

It has following features

smartexecjobdefs stsadm command

An stsadm command that kicks of one-time scheduled jobs and monitors for their completion before returning control
smartsolutionupgrade stsadm command
An stsadm command that performs Solution upgrades in a smart manner
Usage
stsadm -o smartsolutionupgrade [ -filename ] [ -filenamelist ]
Actions performed

1. Accepts as input either a single Solution filename or a filename of a text file containing a list of Solutions to be upgraded
2. Extracts and parses the Solution manifest file from each existing Solution to be upgraded in order to determine the Features that will be affected by the upgrade
3. Inventories the deployment states of the existing Solutions
4. Inventories the activation states of the affected Features at all scopes within the SharePoint Farm
5. Deactivates all affected Features
6. Retracts each of the existing Solutions and deletes them from the Solution store
7. Adds the updated Solution to the Solution store
8. Deploys each of the upgraded Solutions according to their previous deployment state
9. Activates all affected Features according to their previous activation state


Regards,
SharePointKings Team
Read More…



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