Programmatically update Infopath Form XML using c# code

This article would share a code-snippet that updates the Infopath form XML.  In my portal, I’m performing data-acquision using Infopath forms for a Infopath-content type workflow scenario. This scenario is all about an employee appraisal workflow. I’ve a business requirement to mark the ‘IsManager’ column as ‘Yes’ , when the employee gets promoted as Manager. This forced me to think a way to programmatically update the Infopath form XML using c# code. The following is the c# code that can be used to programmatically update the Infopath Form XML.

                        SPWeb   oWeb = SPContext.Current.Web;                       
                        SPList _list = oWeb.Lists[“TestFormLib”];
                        MemoryStream oMemoryStream = new MemoryStream(item.File.OpenBinary());
                        XmlTextReader oReader = new XmlTextReader(oMemoryStream);

                        XmlDocument oDoc = new XmlDocument();
                        oDoc.Load(oReader);

                        oReader.Close();
                        oMemoryStream.Close();

                        XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(oDoc.NameTable);
                        nameSpaceManager.AddNamespace(“my”, “http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-06-11T12:44:57“);

                        doc.DocumentElement.SelectSingleNode(“my:IsManager”, nameSpaceManager).InnerText = “Yes”;
                        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                        SPFile oSPFile = oWeb.Folders[“TestFormLib”].Files.Add(item.File.Name.ToString(), (encoding.GetBytes(doc.OuterXml)), true);
                        item.File.Update();

 

 Subscribe to my blog

Office 365 ondemand web casts

I tried to collate the list of office 365 webcasts that are published this month (July 2011). They are listed below:-

Office 365 Jump Start: Microsoft Office 365 Overview for IT Pros

http://technet.microsoft.com/en-us/edge/video/office-365-jump-start-01-microsoft-office-365-overview-for-it-pros

 

Office 365 Jump Start: SharePoint Online Overview

http://technet.microsoft.com/en-us/edge/office-365-jump-start-12-sharepoint-online-overview

 

Office 365 Jump Start: SharePoint Online Administration

http://technet.microsoft.com/en-us/edge/office-365-jump-start-13-sharepoint-online-administration

 

Office 365 Jump Start: SharePoint Online Extensibility & Customization

http://technet.microsoft.com/en-us/edge/ce-365-jump-start-14-sharepoint-online-extensibility-customization

 

How Microsoft IT does Business Intelligence

http://blogs.technet.com/b/tothesharepoint/archive/2011/06/06/how-does-microsoft-do-bi.aspx

Test Post

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;



namespace Create_Doc_Lib_Activity
{
    public partial class CreateDocumentLibrary : SequenceActivity
    {

        public static DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string), typeof(CreateDocumentLibrary), new PropertyMetadata(""));
        [DescriptionAttribute("Url of base site")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string Url
        {
            get
            {
                return ((string)(base.GetValue(CreateDocumentLibrary.UrlProperty)));
            }
            set
            {
                base.SetValue(CreateDocumentLibrary.UrlProperty, value);
            }
        }


        public static DependencyProperty DocLibNameProperty = DependencyProperty.Register("DocLibName", typeof(string), typeof(CreateDocumentLibrary), new PropertyMetadata(""));
        [DescriptionAttribute("Used as doc lib name")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string DocLibName
        {
            get
            {
                return ((string)(base.GetValue(CreateDocumentLibrary.DocLibNameProperty)));
            }
            set
            {
                base.SetValue(CreateDocumentLibrary.DocLibNameProperty, value);
            }
        }


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

        private void CreateDocLib()
        {
            using (SPSite sps = new SPSite(Url))
            {
                using (SPWeb spw = sps.RootWeb)
                {
                    Guid ID = spw.Lists.Add(DocLibName, DocLibName + " Document Library",
        SPListTemplateType.DocumentLibrary);
                    SPList spdl = spw.Lists[ID];
                    spdl.OnQuickLaunch = true;
                    spdl.Update();
                }
            }
        }



        public CreateDocumentLibrary()
        {
            InitializeComponent();
        }
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Visio 2010 does not show option to create SharePoint 2010 Workflows

I’ve installed the Visio 2010 Professional and tried to design SharePoint 2010 workflow. Surprisingly, i did not get the option to design SharePoint 2010 workflow.

 visio1

I’ve learnt that Microsoft Visio Professional 2010 does not support designing SharePoint 2010 workflows. The solution is to upgrade to Visio Premium 2010 to get the SharePoint Workflow 2010 templates.

visio2

 Subscribe to my post

Silverlight debugging problem: The Silverlight managed debugging package is not installed

I’m involved in developing Silverlight 3.0 development. My environment i s.NET 3.5 SP1, VS 2008 SP1 and Silverlight 3.0. It was working fine until i upgraded the silverlight runtime 4.0. I’m getting this error “Unable to start debugging. The Silverlight managed debugging package is not installed”.

I’ve learned that the fix for this is to install the Silverlight 4.0 install for developers http://go.microsoft.com/fwlink/?LinkID=188039

After installing this hotfix, i could able to debug Silverlight 3.0 applications in VS 2008, with Silverlight 4.0 installed side-by-side.

 Subscribe to my post

Create Custom Tool Parts for SharePoint Web Parts

What is a custom tool part ?

The Custom tool part is part of the web part infrastructure, that helps us to create a custom user interface for the web part properties that goes beyond the capabilities of the default property pane.

When do we need a custom tool part ?

Let’s say, If we need to create a web part property of type dropdown, we need to create a custom tool part. This is not supported out-of-box in the web part framework. I’ve the similar requirement of creating a custom web part property of type drop-down, So i went ahead and implemented the custom tool parts. Here are the steps to create a custom tool part.  

 public class SampleToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        //create an instance of the dropdown control to be used in the custom tool part 
        System.Web.UI.WebControls.DropDownList oDropDown = new System.Web.UI.WebControls.DropDownList();

        // Reference to the parent web part
        SampleWebPart oSampleWebPart = null;

On the constructor method of the tool part class, just set the title of the tool part. In this case i'm going to display the 
vendor names to the user
So i naming the title as select a vendor.
        public SampleToolPart()
        {

            // Set the title for the custom tool part
            this.Title = "Select a Vendor";

        }

        protected override void CreateChildControls()
        {
            try
            {

            //Get the instance of the parent web part on which this tool part is to be hosted
            oSampleWebPart = (SampleWebPart)ParentToolPane.SelectedWebPart; 
            //Get the instance of the current site collection
            SPSite oSPSite = SPControl.GetContextSite(HttpContext.Current);
            //Get the instance of the current site (spweb)
            Guid currentsiteid = SPControl.GetContextWeb(HttpContext.Current).ID;

            using (SPWeb oSPWeb = oSPSite.OpenWeb(currentsiteid))   

                {
In the following few lines of code, i'm reading the list of vendors from the sharepoint custom list 'VendorList' and binding it to the sharepoint custom tool part. 
                    SPList oProviderMaster = oSPWeb.Lists["VendorList"];
                    foreach (SPListItem oSPListItem in oProviderMaster.Items)
                    {

                        string sProviderName = oSPListItem["VendorName"].ToString();
                        oDropDown.Items.Add(sProviderName);
                    }

                }

            // Add the dropdown to the actual toolpart controls
            this.Controls.Add(oDropDown);
            base.CreateChildControls();
            }
            catch (Exception ex)
            {

            }

        }

When the user choses the item from the dropdown of the custom tool part and clicks 'Apply Changes', grab the selected item from 
the dropdown list and assign this value to a public property declared in the parent web part, where the tool part is hosted.
        public override void ApplyChanges()
        {
            try
            {
                if (!(oDropDown.SelectedItem == null))
                {
                    oSampleWebPart.SelectVendor = oDropDown.SelectedValue;
                }

            }
            catch (Exception ex)
            {

               //handle exceptions here
            }            

        }   

    }

 Subscribe to my post

Render Iframes dynamically inside SharePoint Application Pages

There is a requirement to render the publically available web site content inside SharePoint application pages, dynamically based on arbitrary condition. This can be done in two ways. The first option is to use the page viewer web part and dynamically set the url of page viewer using a user control tied to the master page. The second option is to design an application page and render the external content using IFrames. The option of application pages + Iframes is better compared to dynamic page viewer web part way, considering many pros and cons.

The best way to achieve this to have an ASP.NETuser control that hosts the IFrame. Then host the entire user control inside the application page. Because user control gives the more control and flexibility to manipulate the properties of Iframes at run-time, than finding the logical event of ASP.NET page life-cycle and accomplish this task.

.  On the page load of user control, add the src property of the user control

 protected void Page_Load(object sender, EventArgs e)
        {
            this.IFrame1.Attributes.Add("src", ThirdPartyUrl);
        }

 Subscribe to my post

SharePoint Conference 2009 Sessions

The initial list of sessions for SharePoint Conference 2009 to be held in October 2006 at LasVegas is announced. Here is the initial list of session and topics :-

  1. SharePoint 2010 Overview and What’s New
  2. Upgrading to SharePoint 2010
  3. SharePoint 2010 Capacity and Performance Planning
  4. SharePoint 2010 Security and Identity Management: What’s New
  5. Visual Studio 2010 Tools for Office Development
  6. SharePoint 2010 Ribbon, ECMAScript and Dialog Framework Development
  7. Developing with REST and LINQ in SharePoint 2010
  8. Upgrading SharePoint Server 2007 Code to SharePoint 2010
  9. Building Composite Applications with the Microsoft Application Platform
  10. What’s New in Business Connectivity Services (The Business Data Catalog Evolves!)
  11. FAST Search for SharePoint – Capabilities Deep Dive
  12. Advanced Dashboard Creation with Performance Point Services for SharePoint 2010
  13. Overview of Visio and Visio Services for SharePoint 2010
  14. SharePoint 2010 Web Content Management Deep-Dive

We may get more updated sessions down the line.