Showing posts with label SharePoint Designer 2007. Show all posts
Showing posts with label SharePoint Designer 2007. Show all posts

Wednesday, July 11, 2012

Creating custom workflow action for SharePoint Designer 2010



Sometimes we may require creating a custom workflow action when default activity choices offered by designer do not suffice our need.

In this post, I am going to show you how to create a custom action for designer.

You would need Visual Studio for this. So open up VS studio and select Visual C# , empty project in SharePoint 2010.

Give it a name you want.


Deploy it as a farm solution.



Now go ahead and add one more project. This time select workflow – workflow activity library.



Now add two DLL references. Microsoft.SharePoint.dll and Microsoft.SharePoint.WorkflowActions.dll

Now we are going to create three properties which will act as parameters in our custom activity. One will be the site URL, other will be Web Name and the last will be the name of the contact list.

These properties are decorated with certain attributes and declared as dependency 
properties. So go ahead and add three dependency properties.

public static DependencyProperty SiteUrlProperty = DependencyProperty.Register("SiteUrl",

        typeof(string), typeof(CreateContactListType), new PropertyMetadata(""));
        [DescriptionAttribute("Url of site where contact list will be created")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string SiteUrl
        {
            get
            {
                return ((string)(base.GetValue(CreateContactListType.SiteUrlProperty)));
            }
            set
            {
                base.SetValue(CreateContactListType.SiteUrlProperty, value);
            }
        }



        public static DependencyProperty WebNameProperty = DependencyProperty.Register("WebName",

        typeof(string), typeof(CreateContactListType), new PropertyMetadata(""));
        [DescriptionAttribute("Name for Web")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string WebName
        {
            get
            {
                return ((string)(base.GetValue(CreateContactListType.WebNameProperty)));
            }
            set
            {
                base.SetValue(CreateContactListType.WebNameProperty, value);
            }
        }



        public static DependencyProperty ContactListNameProperty = DependencyProperty.Register("ContactListName",

typeof(string), typeof(CreateContactListType), new PropertyMetadata(""));
        [DescriptionAttribute("Name for contact list")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string ContactListName
        {
            get
            {
                return ((string)(base.GetValue(CreateContactListType.ContactListNameProperty)));
            }
            set
            {
                base.SetValue(CreateContactListType.ContactListNameProperty, value);
            }
        }


Now we need to write logic in Execute method as to what needs to be done when this activity executes.
So we are going to create a contact list in specified web with specified name. Remember return  closed as that means activity has successfully completed as a status.


protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            CreateContactList();
            return ActivityExecutionStatus.Closed;
        }

   private void CreateContactList()
        {
            using (SPSite oSPSite = new SPSite(SiteUrl))
            {
                using (SPWeb oSPWeb = oSPSite.AllWebs[WebName])
                {
                    Guid ContactListID = oSPWeb.Lists.Add(ContactListName, ContactListName + " Created From Designer",
        SPListTemplateType.Contacts);

                    SPList contactList = oSPWeb.Lists[ContactListID];
                    contactList.OnQuickLaunch = true;
                    contactList.Update();
                }
            }
        }

We are done with writing our code. It’s time to register the project with strong name. Go ahead in property of this project and give it a strong name.

Now right click the CustomDesignerWorkflowActivity project and add SharePoint mapped folder.



Select Template-1033-Workflow



Now we need to add .actions file which is very important from Designer stand point.

Add new item in the workflow folder and give a name as DesignerCustomActualActivity.actions
This is the file which actually tells SharePoint to make this action available in Designer.

Now add this XML in that file.

<?xml version="1.0" encoding="utf-8" ?>
<WorkflowInfo>
  <Actions Sequential="then" Parallel="and">
    <Action Name="Create Contact List"
        ClassName="DesignerCustomActualActivity.CreateContactListType"
        Assembly="DesignerCustomActualActivity, Version=1.0.0.0,
           Culture=neutral, PublicKeyToken=5e36fcb66a91895d"
        AppliesTo="all"
        Category="SPKings Activity">
      <RuleDesigner Sentence="Contact List Name %1 in %2 within site %3.">
        
        <FieldBind Field="ContactListName" Text="Contact List Name"
           DesignerType="TextArea" Id="1"/>
        
        <FieldBind Field="WebName" Text="Web Name"
           DesignerType="TextArea" Id="2"/>
        
        <FieldBind Field="SiteUrl" Text="Url of base site" Id="3"
           DesignerType="TextArea"/>
      </RuleDesigner>
      <Parameters>
        
        <Parameter Name="ContactListName" Type="System.String, mscorlib"
      Direction="In" />


        <Parameter Name="WebName" Type="System.String, mscorlib"
      Direction="In" />


        <Parameter Name="SiteUrl" Type="System.String, mscorlib"
      Direction="In" />
        
      </Parameters>
    </Action>
  </Actions>
</WorkflowInfo>



If you observe closely, we have defined action in tag and inside it we registered our assembly and defined our own category. We can have the same name in category whenever we create our custom actions so that all our custom action comes under that category.



Then we set the rule designer and in that we have bounded our all three properties with their data type. Then we set the same three as parameters as input because we will be setting the properties.

Now we need to add these into the package. So double click on the package in the project and then click on advanced.




Now add assemblies. You can get this info from GAC If you deploy this application’s DLL in GAC, from there you can get public key token information and then register it here.

If you get an access denied while dragging DLL to assembly or not able to install even through gacutil, then deploy the solution, you will get that in assembly and from there you can get it.

Just a handy tip, there is one more way to get public key token if you do not want to deploy the DLL in GAC and then get the token. Read this post.


Now one more change left and that needs to be done in web.config file of web application. Open up the web.config file of your web application from wss\virtual directory folder and find a tag Authorized Type and add our custom assembly information as mentioned below.




Now build the application and then deploy the application. It’s time to test our custom action in action and to see if it has appeared in designer.

Open designer, connect with the site. Create workflow and click on actions. Here we are with our own custom action.



Click on the create contact list action and we get this. These come from the action file that we created.



Set up the parameters. Save and publish the workflow.

And set up a workflow to run on item adding. Add the item and see the list gets created in the web name you have mentioned in the designer. Execute method gets called when this action gets executed.









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.



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