Monday, February 15, 2010

InfoPath Forms Vs ASPX Pages

In order to create forms for the use of SharePoint Sites, we will be generally using either InfoPath forms or ASPX pages.

In this blog, I am going to mention the pros, cons and the decisions that has to be made at the time of development for the forms for the use in SharePoint sites.

In general, the decision to use among InfoPath forms and ASPX pages all depends on the requirement of the form. Most of the Developers or Managers will think of InfoPath forms for developing the forms to use them in SharePoint Sites as it will be easy to develop and there will be no much coding involved.

Before going for such a decision, we need to understand the requirement and also the pros and cons of using InfoPath forms. Based on the requirements complexity, we need to choose either of InfoPath forms or ASPX pages. Because as the complexity increases, ASPX pages will be the best option for creating the forms. As InfoPath forms by itself are incomplete and needs to have a container (like XMLFormViewer webpart) to be placed in the SharePoint Sites for having same look and feel, which is again in an ASPX page, we need to carefully analyze the requirement and take decisions accordingly.


The following are the PROS and CONS (for and against) using InfoPath over ASPX pages which has to be considered before making a decision.


PROS:
  1. If the requirement is all about showing the data in the form from SharePoint Site, or external data store, InfoPath form is recommended as it does not require much coding.
  2. If we need to submit the data in the form to multiple locations such as SharePoint library, external data store, InfoPath forms are the best solution.
  3. Moreover if there is a need to developing numerous forms which are simple enough and well documented, then InfoPath forms should be considered, as they deliver the solution in a short span as it has drag drop features in it and also provides support for prebuilt template parts and shared data connections.
  4. InfoPath forms are more flexible to use for a quick and neat solution as it has some complex out of the box controls, like Date Picker, File Attachment control, Repeating Sections and also have the code-behind if needed to manipulate the data on the form or behind the form and it also includes a Design Checker to help consistency for forms.
  5. InfoPath forms supports data integrity and version control for document management purposes and add structure to information gathering by converting legacy documents to rich InfoPath form templates and are also very nicely integrated with SharePoint custom workflows as workflow association tool.
  6. It is fully integrated in Microsoft Office 2007 suites like Outlook e-mail messages that can be deployed as Outlook e-mail messages, so collegues can complete the forms without leaving the familiar Outlook environment.
  7. InfoPath forms can easily convert word documents and Excel spreadsheets to forms and build templates to work with.It also creates PDF or XPS and other important document formats and is extensible by addition of a free pulg-in to install third party tools and components for document, archival and records management.
  8. Support for information rights management to help manage permission control and building a powerful document management team site and is also fully centralized for entire Organization and enables Organizations to Centrally.
  9. InfoPath forms are firewall friendly, and the InfoPath Forms Services make it easy to extend the forms solutions beyond the firewall because of using many different web browsers and mobile devices.
  10. InfoPath forms are based on XML and the form is rendered using XSLT stylesheet and the data submitted through the form is available in XML format and hence the data is well structured to retrieve later on and dump the data/forms in some external system. This is in combination with BizTalk Server is a perfect architecture for document transition through multiple levels.


CONS:
  1. If it is a single form and needs complex actions, to and fro between the form and the data store, then choosing Custom ASPX pages is recommended.
  2. A web browser-enabled InfoPath form does not support all features of InfoPath client.
  3. If the requirement needs feature like user going through multiple screens, then Custom ASPX is the solution.
  4. Apart from few complex controls, InfoPath browser enabled forms does not support many advanced controls such as Combo Box, Master/Detail view control, etc., Depending on these limitations, we have to decide on InfoPath forms or ASPX pages. Where ASPX pages have no such limitations.
  5. ASPX can have and do everything that an InfoPath form does with an exception of additional development effort. Apart from it, the deployment process of ASPX forms is much better and easier than InfoPath Forms.
  6. InfoPath Forms are inflexible-difficult or impossible to customize unless you can hack the xsl files of InfoPath.
  7. InfoPath forms will not support SharePoint web services data connections in Forms Based Authentication, image buttons, html, and tree control and is difficult to support Forms Based Authentication(FBA).
  8. Moreover it does not support username() function in FBA as it cannot recognize the current user.
  9. It is difficult to perform automated web test against Forms Services and difficult to support automated deployment as you have to unzip the xsn file and programatically modify the manifest.xsf file, and zip back to the xsn file for automated publishing.
  10. The way that Forms Services supports deployment of InfoPath forms is meaningless as it creates SharePoint Solution Packages with GUID and creates features with meaningless names on the SharePoint Features folder.

Monday, February 8, 2010

Visual Studio 2008 Workflows for SharePoint 2007 Document Libraries

Scenario: There is a SharePoint Document library to which, we are going to associate a custom Workflow which will be developed in Visual Studio 2008. This Workflow will update the status of the Document depending on the percentage edited for a particular document in the Document Library.

Pre-requisites: Microsoft Office SharePoint Server 2007, MS Office 2007, Visual Studio 2008.

Follow the below steps to achieve the Custom workflow which will satisfy the above sceanrio.

Step 1: First Create a Document Library in your SharePoint Site. I have named it as VS2008WFDocLibrary.


Step 2: Create 3 columns with the below mentioned information:


Once you are done creating the Columns, your column settings page will look as follows in columns category:
Step 3: Now from the Document Library page, click New Document.

If you encounter a Fully Trust error msg as follows Click OK.
When the document is opened, save the document in the same Document library by giving a name.

Do not close the document, instead do the following things in the document. The opened document will look like:


Inside the document, type as follows :

Sample Contract Buyer: Seller: Purchase:
Now from the menu, Select Insert tab->Quick Parts->Document Property->then select respective property for respective field such that the final document will appear as follows:

Fill the details in the property fields or in the textboxes provided at the top of the document and then save the document.

Step 4: Start Visual Studio 2008 and Create a New Project by selecting the SharePoint 2007 Sequential Workflow template and give a name to this project. I have given the name as "ContractWorkflow". And then follow the below mentioned steps:





Once you have created the Workflow, Visual Studio 2008 opens the Designer window showing the below fig., Workflow1.cs in design view.


Step 5: If you don't see this, double click the Workflow1.cs file from the Solution Explorer. Now that you see this, drag and drop the Create Task activity which is in SharePoint Workflow tab from the Tool Box. If you don't find this, select the Tool Box from the View Menu.

Step 6: Now configure the properities for CreateTask1 activity as below. To get into the Properites window of CreateTask1, right click on the CreateTask1 and select Properties.

In order to set the properties of the CreateTask1, Click on the respective texboxes for the desired properties.

TaskID property:

TaskProperties property:

MethodInvoking handler: For this you need to type the name in the Textbox provided. I have given the name as myTaskCreation and press enter. This will redirect to Code View of the Workflow1.cs


In this code window, write the following code in the MyTaskCreation event handler and also add the CustomFieldText function.
Now, go back to the Design view of the Workflow1.cs and go to the properties of the CreateTask1 and then give the name to the Correlation Token. I have given the name as "myTaskToken"
Now expand the Correlation Token property to display the OwnerActivityName subproperty and select the value "Workflow1" from the dropdown.

Step 7: Now add the While Activity which will be there in Windows Workflow V3.0 tab to the Workflow from the Tool box and place it as shown below. Its name will be by default whileActivity1.

Now locate the Condition property for whileActivity1 and select the Code Condition as the value.
Now that, expand the Condition property to display the subproperty, type myTaskNotCompleted and press the Enter key. The code window Workflow1.cs opens and in the myTaskNotCompleted event handler, write the following code:


If you look into the code, taskCompleted variable was not declared and so declare the variable in the main class.

private bool taskCompleted = false;

Step 8: Now go back to the Design View and drag onTaskChanged activity which is in SharePoint Workflow tab from the Tool Box and drop it inside the While Activity as shown below. This activity will be named as onTaskChanged1 by default.
Now configure the properities for onTaskChanged1 activity as below. To get into the Properites window of onTaskChanged1, right click on the onTaskChanged1 and select Properties.
Correlation Token property: Set the property to myTaskToken

AfterProperties property: Set the property to afterMyTaskPropertyChange and ensure that the Create Property radio button is selected in the Bind to new member tab.
TaskId property: Set the property to myTaskId in the existing member tab.
Step 9: Now double click on the onTaskChanged1 activity in the design window, the code window opens with the onTaskChanged1 event handler.

write the following line of code in this event handler.

if (afterMyTaskPropertyChange.PercentComplete == 1.0)
{
taskCompleted = true;
}


And thats it, you are now done with the code part. Set a couple of break points for the event handlers ie., for myTaskCreation and onTaskChanged1 event handlers in the code for debugging the process.

Step 10: Press F5 to start the Debug process.
In this process you may find a warning window about script debugging disabled message, Click Yes. It will then be redirected to your SharePoint Document Library that you have created initially. You will be seeing a document lying inside the Document Library. Click on the Context menu of that Item as shown below and select Workflows.

Step 11: You will be redirected to Workflow Settings of the Document Library in which you will be seeing the default workflows and also the workflow that we have created. So Click on the workflow that we have created. In my case it is ContractWorkflow1

Step 12: Now that as the workflow triggers, you will hit the first break point and stops in Visual studio, press F5 to continue. You will be again redirected to the Doucment Library inwhich you will be now seeing a new column "ContractWorkflow1" where it says "Inprogress". Now Click on the Inprogress link to see the status of the workflow.

Step 13: Now click on the Purchase Contract Workflow Task link to display the task. Now observe carefully and you will note that the Description property of the task contains the data that you entered into the Word document for the buyer, seller and purchase price.

Click the edit item to edit the task.

Change the status of the percentage to 50% and click OK. You will be again redirected to Visual studio because you have edited the task and the workflow that we created will trigger when the task is edited or manually triggered and hence we will be redirected to Visual Studio again.

Step 14: Press F10 to debug the code step by step and you will find that the percentage condition 1.0 was not met and now press F5 to continue. You will be again redirected to the SharePoint Document Library. Now repeat Step 13 and this time change the percentage to 100% and click OK. You will be redirected to Visual Studio and now press F10 to debug the code line by line and now that you can see that the percentage condition was met and so it will complete the task and will be redirected to SharePoint Document Library and will show the Workflow status as completed.


Thats it, you have now successfully created the custom Workflow using Visual Studio 2008.

Have fun, write more custom workflows. Will catch you soon with another blog.

Thursday, February 4, 2010

Creating a Basic Webpart from Visual Studio 2005

In this post, I am going to show the basic webpart deployment into SharePoint Site. The purpose of this post is to easily deploy the webpart from Visual Studio 2005 to SharePoint Site.

Pre-requisites: Microsoft Office SharePoint Server 2007, Visual Studio 2005 with Webpart extensions installed.

The aim of the post is not to build a complex webpart, instead, the way to develop and deploy webparts to SharePoint Site. Here, I am going to show, how a feedback data can be submitted from a webpart. In one of my posts, I have explained about submitting the data through InfoPath form and it is the same thing here. The final webpart looks similar to the below image.

In order to achieve this, we need to follow the below steps:

Step 1: In Visual Studio 2005 as mentioned in the pre-requisites, in which Webpart extensions were installed, Create a New Project -> Select Visual C# in Project Types and then Select SharePoint in the extended tree by clicking + icon and then select Webpart template in the templates section, give the name eg., feedbackwebpart to your webpart and then click OK.

Step 2: Now you will be seeing the editor in which feedback.cs file will be opened, if it is not opened, then double click feedback.cs from the solution explorer.
If you look into the code, the default code will be having the sharepoint namespaces already mentioned and also the SharePoint DLL added as references, and in the code you can see that the render method has been placed with comments.

For any webpart, to be deployed into SharePoint Sites, we need basically two methods. They are: Child Controls and Render methods.

Child Controls method is for declaring all the controls that will be used in the webpart, such as Labels, Textboxes, etc.,
Render method is for rendering all the controls in the sharepiont site.



Initially, instead of implementing the code completely, we will follow the baby steps to achieve this.

First of all, I will deploy the very first part of the webpart ie.,


Because, the webpart once deployed, through this webpart extensions installed in the Visual Studio 2005, will be easy and doesn't require any dll placing or safecontrols adding in web.config files. Everything will be taken care by the Deploy process only. Hence we will first achieve to deploy the first part of the webpart as mentioned above.

Step 3: In order to achieve the first part, we need a table and a row and a column and along with it, one label.

  • Firstly we will declare the Label variable inside the main class as follows:

Label lblformHeader;

  • Then in the Child Controls method we have define the Label control, so that it can be used in the Render method.
protected override void CreateChildControls()
{
Label lblformHeader = new Label();
lblformHeader.ID = "lblformHeader";
lblformHeader.Text = "Feedback Form";
this.Controls.Add(lblformHeader);
}
  • Now that, as we have defined Label for form header, we can render that control. So, we can now use the code in the render method as follows:
protected override void Render(HtmlTextWriter writer)
{
writer.Write("");
writer.Write("");
writer.Write("
");
lblformHeader.RenderControl(writer);
writer.Write("
");
}

  • Now we have to go to the properities page of the project by right clicking on the project name in the Solution explorer and then select Properities and in the Properities windown make sure that in the Debug section, Start Browser with URL should be your Site where you want to deploy the webpart. And also make sure that in the Signing section, you select signing Key if it does not exists for strong naming the assembly.


Thats it you are now done with the first part. Now that, you only need to deploy the Solution by right clicking on the project and selecting Deploy. As shown below.

Once, the deployment was done, successfully, you will need to add the webpart in the desired location of your mentioned site from Edit page option in your sharepoint site. You will be seeing the first part in your site as follows:

Now that, as we had the first part, we will now write the code to achieve the complete design as below:


Follow the below steps, to achieve this:


  • Firstly we will declare the variables inside the main class for the rest of the controls that has to be placed in the webpart as done previously: The code declaring the variables as a whole looks like this:

  • Secondly declaring the Child Controls. And the code as a whole, looks as follows:

  • Next comes the part of Render method. The code as a whole looks like this:

  • And finally comes to Button Click Event handler.
Now run the code by clicking on F5 or right click the project and select Deploy.

And thats it, you are done. Your final webpart looks like this:


This, completes the webpart deployment.


Enjoy the coding. Will catch you soon in my next blog.

Submitting Data to SharePoint List Through InfoPath Form

Pre-requisites: InfoPath 2007, Microsoft Office SharePoint Server 2007, Visual Studio Tools for Applications.

In this post, I am going to detail about how to submit the data from an InfoPath form to a SharePoint List.

In order to detail this, I am taking an example of a feedback form, where in the end users will fill the form and submit, and upon submitting the form, the details entered in the form will be submitted to a SharePoint List.

Step 1: First of Create a Custom List in a SharePoint Site. I have named it as Feedback. Now create some columns as per your feedback form. I have created Username, Subject, Description, and EmailID as columns in which all the columns are of type Single text.

Step 2: Now open InfoPath Form 2007, and follow the below steps which has to be done in InfoPath Form.

  1. In Getting Started Wizard, Select Design a Form Template.

  2. In Design a Form Template, make sure that, Form Template -> Blank -> and finally Check box saying that "Enable Browser-Compatible features only" are Selected.

  3. Now when the Form Opens, see the Design Tasks Pane, if you don't see this pane, you can select it from Menu options from View and select Task Pane or press F1. Most of the tasks that we perform in designing the InfoPath form are from here in the Design Task Pane.


  4. Click on Layout in Design Task Pane and then click on Custom Table... link.


    And a window pops up requesting for number of Columns and Rows. Enter 3 and 6 respectively.

  5. And the table appears in the design window in which you have to add Text boxes and Rich Text box from the Design Task Pane in Controls, so that the form as a whole should appear as follows.

  6. Now Click on Data Sources in Design Task Pane. Name the Data Sources as shown below. The names should be appropriate to the labels mentioned such as Username holds datasource nameDS, Subject -> subjectDS, Description -> descriptionDS, Email ID -> emailDS respectively.
  7. You will now see one more datasource that is been added in the Data Source which is listID. To get this, right click on myFields and select Add.


    There you do the following. You will be seeing a number in the default value ie., "9FE04D9C-1C6E-4A96-8FA1-785C878DD0F5".




    This is the SharePoint list ID, which you will be getting it from SharePoint Site. In order to get this ID, Click on Site Actions -> Site Settings -> Modify All Settings, and then select "site libraries and lists" in the 'Site Administration' category.


    Then select your list name and if you look into the URL, you can find the list ID. ie., http://mossdev2:3672/_layouts/ListEdit.aspx?List={9FE04D9C-1C6E-4A96-8FA1-785C878DD0F5}&Source=http%3A%2F%2Fmossdev2%3A3672%2F%5Flayouts%2Fmcontent%2Easpx. This bold value is the listID in the URL. Copy that value and paste it in the default value.
  8. Now change the Button name, ie double click on the button place in the table. Change the name of the button to Submit, as shown below.
  9. Now we should create Data Connections. For this example, we need two dataconnections, one is to submit the data to SharePoint list and the other is an XML data connection which is used to match the InfoPath form Data Sources to SharePoint List Columns.
DataConnection to match the InfoPath Form Data Sources to SharePoint List Columns:
  1. Open a Notepad and write the following code in it.
  2. Code:
    If you observe, this code, first line is all about the version of XML and the accepted encoding value, which will be the same and then there is a Batch tag in which we are defining Methods that has ID and Command(Such as Update, New,Delete etc.,) and within which we have mentioned the Field names which represent SharePoint List Column names.
  3. Now save this file with extension as .xml. I have saved the file name with feedback.xml
  4. Now go back to InfoPath, and in the menu, Click on Tools -> DataConnections and then Click Add in the Data Connection Wizard. Select 'Create a new connection to:' and then select Recieve data and Click Next. Select XML document and then click Next, and finally browse the file feedback.xml which you have created in Step 2 and click Next. Select the option 'Include the data as a resource file in the form template or template part' and click Next. Give a name to this Data Connection, I have given the name as feedback.

    We are now done, Creating one Data Connection.

DataConnection for Submitting the Data
: In the men, Click on Tools -> DataConnections and then Click Add in the Data Connection Wizard.
  1. Select the option Create a new Connection to: and click Next, Select To a Web service to submit your data. And in the "Enter the location of the Web service you want users to submit their forms to: " add the URL of your SharePoint Site, ie., http://<mossdev2:3672>/_vti_bin/lists.asmx?WSDL this locates the webservice for your sharepoint site in which you want to submit the data and then Click Next. Please Note: the above url has to be replaced with your SharePoint Site Collection Url. ie., <mossdev2:3672> has to be replaced with your SharePoint Site Collection URL.
  2. You will now be showing a list of web services, as our goal is to submit the data, we are going to select the option UpdateListItems option and then click Next. You will be seeing the Parameters list in which you will be having tns:listName and tns:udpates. Double click on tns:listName and here you have to specify the listname which InfoPath form is referering . Initially we have created a Datasource by name listID in which we have refered to SharePoint list by its listID and hence we are going to give that datasource here and hence select the DataSource listID and click OK. Now Double Click on tns:updates, and in the Data Source window, select the previously created Data Connection that is feedback and then select Batch and click OK.
  3. Next in the Parameter Options, Select the Field or Group that refers to listID and in the Include:, you have to select XML subtree, including selected item and click Next.
Thats it, you are now done, creating the 2 Data Connections.

Now it is the time for us to talk about the Submit Button in the form. If you double click the form, it will open up the properties window, in which you have to click on edit form code.


This will redirect to VSTA editor. if it is showing an error such as the following:


It means that you haven't installed the VSTA. You have to install VSTA in order to perform coding in InfoPath forms. For directions to installing VSTA, Click here...

Once it is redirected to VSTA editor, the code inside will look like the following..


Write the following code in Button Click Event "CTRL1_5_Clicked"
try
{
XPathNavigator root = MainDataSource.CreateNavigator();

// Retrieve the values for the separation list item
string listID = root.SelectSingleNode("/my:myFields/my:listID", NamespaceManager).Value;
string nameDS = root.SelectSingleNode("/my:myFields/my:nameDS", NamespaceManager).Value;
string subjectDS = root.SelectSingleNode("/my:myFields/my:subjectDS", NamespaceManager).Value;
string descriptionDS = root.SelectSingleNode("/my:myFields/my:descriptionDS", NamespaceManager).Value;
string emailDS = root.SelectSingleNode("/my:myFields/my:emailDS", NamespaceManager).Value;

if (listID == null || listID == "" || nameDS == "" || subjectDS == "" || descriptionDS == null || emailDS == null)
{ }
else
{

//This is CAML xml file. it contains batch and method nodes
XPathNavigator batch = DataSources["feedback"].CreateNavigator();

batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']", NamespaceManager).SetValue(nameDS);
batch.SelectSingleNode("/Batch/Method/Field[@Name='Subject']", NamespaceManager).SetValue(subjectDS);
batch.SelectSingleNode("/Batch/Method/Field[@Name='Description']", NamespaceManager).SetValue(descriptionDS);
batch.SelectSingleNode("/Batch/Method/Field[@Name='EmailID']", NamespaceManager).SetValue(emailDS);

DataConnections["Web Service Submit"].Execute();
}
}
catch (Exception ex)
{
ex.Message.ToString();
}


Now that you are done with coding. You now need to preview the form, by clicking preview, and you will see the form, fill the details and click on submit. The details will be submitted in the SharePoint list.

Once this testing is done, you can now Publish this InfoPath form to the SharePoint Site.

Hope you like this post. Have a nice day. Will come up with another post sooner.



If interested, you can have a look at Visual Studio Workflows which was explained clearly by Zaltan's blog at http://dotnet.org.za/zlatan/archive/2007/09/15/how-to-create-a-sharepoint-2007-workflow-using-visual-studio-2008-beta-example.aspx

Requirements: http://walaapoints.blogspot.com/2007/02/custom-workflow-with-visual-studio-2005.html

Followers