InfoPath Rules,Validation, Conditional formatting,Code Behind- a sample application for timesheet-part2


Okay from the last post we saw that we could open code behind and saw VSTA opening up from the code location you know J

Before we deep dive into doing InfoPath Code behind go through my last post which explains how to deploy , develop and debug infopath forms with code behind deployed as content types.

Let’s make a few changes on the form buttons for better look and feel and functionality.

Add 2 new sections “ApproverContentActionSection” and “RejectReturnContentSection” below “ContentActionSection” and move the buttons “Approve” to and “Reject” and “Return” to the “RejectReturnContentSection”.

 

Also for “WithDraw” button we will move it to a separate section “contentWithDrawSection”

image

image

Let me also show you the revised conditional formatting for the buttons and their sections

Submit button’s section “ContentActionSection” and on the flags below set “Hide this Control”

image

Submit Button conditional formatting:

image

WithDraw section’s conditional formatting for “Hide this control”

image

Approve button’s “ApproveContentActionSection”

image

For Reject and Return buttons section “RejectReturnContentSection”

image

Going back to code behind of the “OK” button clicked

  • We need to use SP object model to get the user name.
    //get current user

    string strAccountId = SPContext.Current.Web.CurrentUser.Name;

 

  • Make sure that you had added the reference Microsoft.SharePoint.dll and added  
    using Microsoft.SharePoint;

  • Now we need to get the value of the password field. Use the method from my post here and copy xpath expression from the designer and place a constant as below

image

const string PASSWORD = "/my:myFields/my:time/my:signtimesheet/my:password";

 

The copy xpath value will come from your InfoPath designer as below

You can now put the code for getting the password

//get password from field values

string strPassword = GetFieldValue(PASSWORD);

You can now validate the username and password credentials

bool valid = false;

using (PrincipalContext context = new PrincipalContext(ContextType.Domain))

{

      valid = context.ValidateCredentials(strAccountId, strPassword);

}

Now get the “SetFieldValue” from the here and paste it in the form code

  • Set the field value for “IsAuthenticated” and for the username , so that we know the user who signed the document gets to know that the document was actually signed.
if (valid)

            {

                SetFieldValue(ISAUTH, "1", false);

                SetFieldValue(USERNAME, strAccountId, false);

                

            }

            else

            {

                SetFieldValue(ISAUTH, "0", false);

                SetFieldValue(USERNAME, "Invalid Password. Please enter correct password", false);

            }

 

  • Now on the submit button, set the actions to submit the form and perform operations through code and close the form.

image

  • Before we actually start writing code for “Submit” button we need to fetch few values from input parameters of the form.
    if (e.InputParameters.ContainsKey("SaveLocation"))

                {

                    SAVELOCATION = e.InputParameters["SaveLocation"];

                }

                if (e.InputParameters.ContainsKey("Source"))

                {

                    SOURCE = e.InputParameters["Source"];

                }

                if (e.InputParameters.ContainsKey("XmlLocation"))

                {

                    XMLLOCATION = e.InputParameters["XmlLocation"];

                }

 

 

  • Also since I need to hide the sections for “Approve”, “Reject , Return” , I need to set some field values based on the user belongs to a specific SharePoint Group which would bind to a specific permission level.

SPAPPROVERGROUPNAME is a constant for the SharePoint group name

using (SPSite site = new SPSite(SOURCE))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        isApprover = web.IsCurrentUserMemberOfGroup(web.Groups[SPAPPROVERGROUPNAME].ID);

 

                        if (isApprover)

                            SetFieldValue(ISAPPROVER, "1", false);

                            

                    }

                } 

  • Okay now coming back to “Submit” Operation I will set a few field values so that if the timesheet needs to be submitted after the withdraw operation
//set submitted status and flag fields

                SetFieldValue(STATUS, SUBMITTED, false);

                SetFieldValue(ISSUBMITTED, "1", false);

 

                //set password field before submitting for security reasons

                SetFieldValue(PASSWORD, string.Empty, false);

                SetFieldValue(ISAUTH, "1", false);

  • Now I would have a filename based on the user who is logged in and on date and time he would be submitting the timesheet.
string fileName = SPContext.Current.Web.CurrentUser.Name

                                        + "_" + DateTime.Now.ToShortDateString()

                                        + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second;

 

                fileName = fileName.Replace("\\", "_");

                fileName = fileName.Replace("/", "_");

  • I have to save this file to a SharePoint form library and need to make sure that when the file is submitted again the last file is updated. The “XmlLocation” from the input parameters has the file name when it’s submitted again, since it was saved to the form library in the first submit operation
if (string.IsNullOrEmpty(XMLLOCATION))

                {

                    siteLocation = SAVELOCATION;

                }

                else

                {

                    siteLocation = XMLLOCATION;

                }

 

                try

                {

 

                    using (SPSite site = new SPSite(SOURCE))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

 

                            web.AllowUnsafeUpdates = true;

                            

                            string[] values = siteLocation.Split('/');

 

                            if (String.IsNullOrEmpty(XMLLOCATION))

                            {

                                if (siteLocation.EndsWith("/"))

                                    strDocLibName = values[values.Length - 2];

                                else

                                    strDocLibName = values[values.Length - 1];

                            }

                            else

                            {

                                if (siteLocation.StartsWith("/"))

                                    strDocLibName = values[values.Length - 2];

                                else

                                    strDocLibName = values[values.Length - 1];

                            }

                            SPFolder doclib = web.Folders[strDocLibName];

 

 

                            if (String.IsNullOrEmpty(XMLLOCATION))

                            {

                                fullfilelocation = doclib.Url + "/" +

                                              fileName + ".xml";

                            }

                            else

                            {

                                fullfilelocation = XMLLOCATION;

                            }

                            SPFileCollection destFiles = doclib.Files;

 

                            XPathNavigator xPathNav = MainDataSource.CreateNavigator();

 

                            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

 

                            byte[] Data = encoding.GetBytes(xPathNav.OuterXml);

 

                            destFiles.Add(fullfilelocation, Data, true);

 

                            doclib.Update();

 

                            web.AllowUnsafeUpdates = false;

                        }

                    }

  • Shall we see how it all works now with the code behind in InfoPath forms and now this site would replace legacy of your organization to SharePoint and InfoPath forms. J
Advertisements

Trackbacks/Pingbacks

  1. Website - July 29, 2011

    Website…

    InfoPath Rules,Validation, Conditional formatting,Code Behind- a sample application for timesheet-part2 « Atul Chhoda's Blog…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: