The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Drool, Britannia? Is the UK Failing the Cloud?
By Roger Strukhoff
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Jan. 8, 2012 11:38 AM EST
read more & respond »
Cloud Expo on Google News
Did you read today's front page stories & breaking news?

Cloud Expo & Virtualization 2011 West
Keynotes
Oracle
Opening Keynote | An Enterprise Cloud for Business-Critical Applications
Abiquo
Day 2 Keynote | The Enterprise Cloud Tightrope - Balancing for Success
Akamai
Day 3 Keynote | The DNA of an Enterprise Cloud
DIAMOND SPONSOR:
Oracle
Many Clouds, Many Choices'Cloud
PLATINUM PLUS SPONSORS:
Abiquo
Enterprise Cloud Best Practices - Town Hall - Join the discussion…
PLATINUM SPONSORS:
Intel
Progressing Toward the Federated, Automated and Client-Aware Cloud
New Relic
How to build an app with Twitter-like throughput
Rackspace
Computing in the Cloud Era
GOLD SPONSORS:
Gale Technologies
Practical Cloud Migration
IBM
Re-think IT. Re-inventing Business.
Intel/McAfee
Identity Driven Security in the Cloud
PerspecSys
Hackers Hackers Everywhere, Is My Public Cloud That Safe?
Red Hat
Unlock the Value of the Cloud
SHI
Mission Critical Applications and the Cloud - Myth or Reality?
SoftLayer
Not Your Grandpa's Cloud
Terremark
Integrating Enterprise Clouds
VMware
Upgrade to a vCloud
POWER PANELS:
Cloud Expo Silicon Valley: CTO Power Panel
Cloud Expo Silicon Valley: CEO Power Panel
Cloud Expo Silicon Valley: Cloud SuperStars Panel
Cloud Expo Silicon Valley: CloudNOW Panel
Click For 2010 West
Event Webcasts
Cloud Expo & Virtualization 2011 East
DIAMOND SPONSOR:
Dell
Dell & VMware Deliver the Enterprise Hybrid Cloud
PLATINUM PLUS SPONSORS:
Abiquo
Are Financial Services Organizations Risking Security by Avoiding Cloud Computing?
Oracle
From Consolidation to Enterprise Private PaaS
PLATINUM SPONSORS:
Intel
Driving the Transformation to Next Generation Cloud Data Centers
Rackspace
The Inevitability of an Open Cloud
GOLD SPONSORS:
CA Technologies
Follow YOUR path to Cloud Computing
Interxion
Who Keeps the Cloud in the Air?
Microsoft
Patterns for Cloud Computing
PerspecSys
War in the Clouds: Are you ready?
ServiceMesh
The Big Win: Stop Playing Small-Ball with Your Cloud Strategy
Terremark
Evaluating Enterprise Clouds
Xiotech
Cloud Storage: Myths and Realities
POWER PANELS:
Cloud Expo New York: CTO Power Panel
Cloud Expo New York: CEO Power Panel
Cloud Expo New York: CMO Power Panel
Cloud Expo New York: Wrap-Up Power Panel
Click For 2010 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


Automatic Creation of InfoPath forms - not as easy as I thought

By: Andrew Gelina
Jan. 5, 2009 08:29 PM

I recently completed what I estimated to be a small project of taking an existing set of InfoPath forms that represented Content Types and Site Columns for a client's yearly employee review process.  Last year they had to manually create a number of forms for each employee to be filled out and eventually sent to Human Resources.  If each employee has an average of 6 forms (they do peer reviews) and a manager is required to pre-fill each form and manage the permissions of said form for each employee, a significant amount of time is wasted. 

I estimated that in about a week or two we could write a web part to collect all the data, load up and pre-fill each form's XML, add it to the doc library, and set the permissions for the forms throughout their lifecycle based on the current status of the review process.  It will take too long to go over the whole project/code-base, so I'll highlight the important areas.

1.   Web Part to collect employee, supervisor, reviewer users and some dates, etc.

 Untitled picture

2.  Event Handler on the form library to look at the status as updated by the form upon submit.

Problem 1:

I thought I could be clever.  Knowing the XSN generated from InfoPath contained the underlying XML file I would need to populate and save in SharePoint, I thought I could use the foundation from this blog post to get to that file:  http://www.kindohm.com/archive/2008/01/10/programmatically-create-an-infopath-form-instance-from-xsn-template.aspx

It is a great concept, albeit a little hokey.  The basic concept is to use the XMLFormView control to load an instance of an XSN, then extract the XML from it and use it.  The problem is that you can't get the control to initialize unless it is actually run in a page.  So, the poster put the form in another aspx page and calls it via his code (in my case the web part) through the WebClient or HTTPWebRequest and return it as a Response Type of XML.  This solution works great in Windows Server 2003, but Windows Server 2008 it only throws 401 Unauthorized errors.  It tried everything, every different type of credential I could.  The only time it worked in Server 2008 is when I used the default app pool in IIS, and it has some different settings (Network service account, etc) that I wouldn't want to use for MOSS.  At any rate I could not get around this problem even after hitting up some MOSS MVPs that I have tremendous respect for.

Problem 2:

I worked around Problem 1 (hopefully temporarily) by saving my InfoPath forms as source files and manually dropping a copy of the XML container in the form library where the XSN lives.  This created Problem 2.

If you create your InfoPath form on your local machine, the resulting XML does not share the same XML processing instructions as those that are created when publishing to a SharePoint library.  So, I had to manually replace the instructions in each XML file as it was created.  I had to use scientific method to find out which instructions were causing the form to load improperly, but it came down to these two items:  href and solutionversion.  Href is obvious, the xml needs to point to the library that is hosting it so Forms Server knows where to process the form.  Until it is published to the library this value is not set, so I had to set it by hand.  Code snippet below:

XmlNode oldPiNode = xmlForm.SelectSingleNode("/processing-instruction(\"mso-infoPathSolution\")");

           
// delete a processing instruction
            XmlNode ndDel = xmlForm.SelectSingleNode("/processing-instruction(\"mso-infoPathSolution\")");
           
xmlForm.RemoveChild(ndDel);

           
// create a new processing instruction
            Match mHref = Regex.Match(oldPiNode.Value, @"href=\""(.*?)\""");
           
if (mHref.Success)
           
{
               
oldPiNode.Value = oldPiNode.Value.Replace(mHref.Groups[1].Value, "http://xxxx.xxxx.com/review/Review/Forms/" + infoPathTemplateName);
           
}

Solutionversion turned out to be completely obnoxious.  By default, the solution version in the "save as source" XML is exactly 3 revisions ahead of the one published to SharePoint.  So I needed to manually decrement my XML file so Forms Server could properly load it: 

Match mSolVer = Regex.Match(oldPiNode.Value, @"solutionVersion=\""(.*?)\""");
           
if (mSolVer.Success)
           
{
               
string solVer = mSolVer.Groups[1].Value;
               
int smallver = Convert.ToInt32(solVer.Substring(6));
               
int smallverDecremented = smallver - 3;
               
oldPiNode.Value = oldPiNode.Value.Replace(smallver.ToString(), smallverDecremented.ToString());
           
}

Since processing instructions are just whole single nodes, I couldn't easily just replace individual attributes.  Instead I had to grab the existing entire node and replace those elements I needed to update via string manipulation, delete and re-add my new processing node back into the XML.  This all seemed to work, and can be completely removed if I can get the security issue in Problem 1 fixed at a later time.

Problem 3:

This problem isn't specific to InfoPath or this solution, but has come up enough that some people new to this game should be aware of.  If you want to give a set of users rights to add items to a document library, but then only see and manage their own items, you can't do it.  It works fine with standard lists (an advanced setting on Lists) but this was left out for Document Library and child lists.  Why?  I'm not sure. 

It certainly makes it difficult on projects such as these where you need to assign users certain items, and list item permission by hand can be tedious and error-prone. 

My common solution to this problem (when I don't have workflow as my manager of the data) is to use an event handler on the list to look at the appropriate metadata and content type and determine what needs to happen.  It is sort of a mini rules engine for permissions processing and it looks like this:

First, override your events (ItemAdded and ItemUpdated for example) and check the Content Type: (feel free to use more descriptive terms, the names have been changed in my code to protect the innocent)

public override void ItemUpdated(SPItemEventProperties properties)
       
{
           
base.ItemUpdated(properties);

            switch (properties.ListItem.ContentType.Name)
           
{
               
case "Part1":
                   
UpdateFormOne(properties);
                   
break;
               
case "Part 2":
                   
UpdateFormTwo(properties);
                   
break;
               
case "Part 3":
                   
UpdateFormThree(properties);
                   
break;
               
case "Part 4":
                   
UpdateFormFour(properties);
                   
break;
               
case "Part 5":
                   
UpdateFormFive(properties);
                   
break;
               
default:
                   
break;
           
}
       
}
 
What does the Update code look like:
 
private void UpdateFormFive(SPItemEventProperties properties)
       
{

           
string formStatus = properties.ListItem["Form Status"].ToString();

           
XmlDocument xmlForm = new XmlDocument();
           
Byte[] fileBytes = properties.ListItem.File.OpenBinary();
           
MemoryStream stream = new MemoryStream(fileBytes);
           
xmlForm.Load(stream);

           
XmlElement root = xmlForm.DocumentElement;

           
string evaluatorUserKey = root["my:employeeUserKey"].InnerText;
           
string supervisorUserKey = root["my:supervisorUserKey"].InnerText;

           
SPUser evaluatorUser = properties.ListItem.Web.SiteUsers[evaluatorUserKey];
           
SPUser supervisorUser = properties.ListItem.Web.SiteUsers[supervisorUserKey];

           
List<SPUser> userList = new List<SPUser>();

           
switch (formStatus)
           
{
               
case "Not Started":
                   
userList = new List<SPUser>();
                   
userList.Add(evaluatorUser);
                   
userList.Add(supervisorUser);
                   
Utilities.RemoveContributePermissions(properties.SiteId, properties.ListItem.Web.ID, properties.ListId, properties.ListItem.ID, userList);
                   
Utilities.BreakandSetContributePermissions(properties.SiteId, properties.ListItem.Web.ID, properties.ListId, properties.ListItem.ID, userList);
                   
break;
               
case "Started":
                   
break;
               
case "Complete":
                   
break;
               
case "Submitted to HR":
                   
userList = new List<SPUser>();
                   
userList.Add(evaluatorUser);
                   
userList.Add(supervisorUser);
                   
Utilities.RemoveContributePermissions(properties.SiteId, properties.ListItem.Web.ID, properties.ListId, properties.ListItem.ID, userList);
                   
Utilities.BreakandSetReadPermissions(properties.SiteId, properties.ListItem.Web.ID, properties.ListId, properties.ListItem.ID, userList);
                   
break;
               
default:
                   
break;
           
}
           
stream.Close();
       
}
 
Some explanation needs to be provided here:
 
I load up the actual form because I stored some data in there.  Without workflow, I needed a location to manage the users that will need access at various points, and the form itself proved to be a nice container.  evaluatorUserKey and supervisorUserKey are used to create SPUser objects to pass to the permissions methods.  I then look for the current status to determine what permissions to set.

Here is what the permissions setting code looks like:

internal static void BreakandSetContributePermissions(Guid siteID, Guid webID, Guid listID, int listItemID, List<SPUser> userList)
       
{
           
SPSecurity.RunWithElevatedPrivileges(delegate()
           
{
               
using (SPSite site = new SPSite(siteID))
               
{
                   
site.AllowUnsafeUpdates = true;
                   
using (SPWeb web = site.OpenWeb(webID))
                   
{
                       
web.AllowUnsafeUpdates = true;
                       
SPList list = web.Lists[listID];
                       
SPListItem item = list.GetItemById(listItemID);

                       
try
                       
{
                           
if (!item.HasUniqueRoleAssignments)
                           
{
                               
item.BreakRoleInheritance(false);
                           
}

                           
foreach (SPUser user in userList)
                           
{
                               
HandleEventFiring eventFiring = new HandleEventFiring();
                               
eventFiring.CustomDisableEventFiring();
                               
item = SetItemLevelPermissions(item.Web, item, SPRoleType.Contributor, user.ID);
                               
item.SystemUpdate();
                               
eventFiring.CustomEnableEventFiring();

                           
}
                       
}
                       
catch (Exception ex)
                       
{
                           
//Add error handling code here
                        }
                   
}
               
}

           
});
       
}

This code is pretty straightforward except for HandleEventFiring which is something none of you have seen before,  This is a custom class I created to handle versioning issues on list items, etc.  My full blog post about how and why I use it is here:  http://blogs.syrinx.com/blogs/sharepoint/archive/2008/11/10/disabling-event-firing-on-splistitem-update-differently.aspx

And finally, the code to set the actual permissions:

        private static SPListItem SetItemLevelPermissions(SPWeb setSPWeb, SPListItem setListItem, SPRoleType setRoleType, int userID)
       
{

           
SPUser user = setSPWeb.SiteUsers.GetByID(userID);
           
SPRoleDefinition roleDefinition = setSPWeb.RoleDefinitions.GetByType(setRoleType);
           
SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, string.Empty, string.Empty, string.Empty);
           
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
           
setListItem.RoleAssignments.Add(roleAssignment);
           
return setListItem;
       
}

That's my solution for one way (without custom workflow) to manage document permissions to allow everyone to contribute, but then lock down the documents upon creation and updating.

Problem 4:

Another concern with InfoPath I ran into was publishing updated forms to SharePoint and mapping fields to site columns.  InfoPath supports creating site columns locally and mapping them to it's local XML data fields to be updated automatically upon submission. 

Upon publishing changes to a test environment I didn't realize that it is possible for these fields to get out of sync.  InfoPath protects itself by creating brand new site columns with the same name.  From the SharePoint UI and InfoPath perspective everything looks great until you see your column names showing up twice in list management and view creation screens.  Your code also starts breaking as it's looking for a certain internal name that is no longer being populated.

Basically SharePoint just creates a new site column with the same display name, but creates an internal name like columnname1 or columnname12.  The fix for this is DON'T DO IT IN THE FIRST PLACE.  It is a pain to hunt down the correct columns and delete them (after deleting your content type form because of the "in use" issue) 

Instead, at this screen:

Untitled picture2

Click "Modify" and make sure the local field is mapped to your correct site column.  If you see (none:  create new site column) you need to change it unless this is a brand new field.

The screen below shows how to "re-map" to an existing site column created by InfoPath:

Untitled picture3

Well, I know this post was long, and it didn't cover everything I did, but I tried to cover the parts that caused me some hang-ups.  Despite losing 2.5 days alone to the security problem I still was able to deliver the mini-project on time.  There were some things I would have much rather done differently, but due to the issues and time constraints I had to get something out there and working.

I hope this helps,

-Ryan Thomas

Read the original blog entry...

Published Jan. 5, 2009— Reads 1,242
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Andrew Gelina
Andrew Gelina brings over 12 years of software architecture and development experience to his role as CEO of Syrinx Consulting, where he is responsible for the strategic direction, technology focus, operations management, and growth of the firm.

Prior to joining Syrinx in 2003, Andrew helped build Web Technology Partners into a leading software engineering consulting firm before selling it in 2000 to Monster.com, the global online career and recruitment resource. During the next three years at Monster, he developed software and managed projects for virtually every area of Monster's operations, from CRM integration to e-commerce to high-traffic, high-volume Web development. He also worked closely with Microsoft to scale its .NET platform to Monster's huge transaction volumes.

Andrew has also worked in several other areas of technology leadership, performing technical due diligence for companies considering acquisitions and selling professional services. He started his career at EDS, helping them develop cellular billing and switch interface software to support the emerging wireless industry.

He graduated cum laude from the University of Massachusetts at Amherst, where he received a bachelor's degree in operations management. Andrew is a member of the CEO Roundtable of the Massachusetts Technology Leadership Council.

Andrew and his 35-member team work on-site with clients all over New England.

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
Muzzling of Canadian Government Scientists on Agenda at Top International Science Conference
NASA Selects California Teacher Kelly Maxey to be "Agent of Change" for STEM Education
GE To Open New Technology Center in New Orleans
NASA Selects California Teacher Edita Lara-Vasquez To Be "Agent of Change" for STEM Education
Isle of Capri Casinos, Inc. Will Report FY 2012 Third Quarter Results
NaviNet Highlights Care Collaboration Through a Panel Discussion: Delivering Value to the Patient
Media Advisory for Mendocino Wine Company
2012 Sapphire Award Recognizes Excellence in Community Health
Rural Florida Communities Partner with RelayHealth to Create a Health Information Exchange
Zebra Technologies Announces Investor Day on February 22, 2012, in Chicago

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window