programming4us
programming4us
WEBSITE

InfoPath with SharePoint 2010 : Add Changed Event Code for Tracking Changes

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
6/25/2014 9:49:04 PM
Scenario/Problem:You need to add changed event code for tracking changes to your field.

Solution:Select the field and click Changed Event on the Developer ribbon.

The next step for tracking changes is to add the code-behind to facilitate the storing of the changes. The code executes when the text in the text boxes changes. To add the code-behind, follow these steps:

1.
Select the field (Description in the example) on the form.

2.
On the Developer ribbon, click Changed Event. The Code Editor appears.

3.
Add the Microsoft.SharePoint assembly as a reference to the project, as shown in Figure 1. This allows you to access a special difference utility.

Figure 1. Adding a reference to SharePoint allows you to access a special difference utility.


4.
Add System.Web as a reference such that the HTTP Context may be used.

5.
Add the proper using statements at the top of the FormCode.cs to reference the SharePoint utility and System.Web, as shown in Listing 1.

6.
In the Changed method, add a string declaration for the namespace and the username, as shown in Listing 2

7.
Populate the userName variable with the current user filling out the form by using the HTTP context (Listing 3).

8.
Declare color variables for the types of changes as shown in Listing 4. The color codes are HTML-based color codes.

Listing 1. Using References
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;

Listing 2. String Declarations
//Get namespace
string myNamespace = NamespaceManager.LookupNamespace("MY");
string userName = string.Empty;

Listing 3. Populate userName
if (HttpContext.Current != null)
{
userName = HttpContext.Current.User.Identity.Name.ToString();
}

Listing 4. Color Variable Declarations
 //Color Variables
string changeColor = "#330066";
string deleteColor = "#FF0000";
string insertColor = "#008000";

Tip

Declare the color variables as constants if you are tracking changes on multiple text boxes.


You will now use the SPDiffUtility provided by the SharePoint assembly to produce the track changes results. This is facilitated by setting up the Open and Close tags using HTML strings and then calling the Diff method from the utility. This code is shown in Listing 3. At this point, your changed method should look similar to Figure 2.

Figure 2. The SPDiffUtility produces the changed values based on the configured tags.

Listing 3. SPDiffUtility Code
//Set the tags
SPDiffUtility.ChangeOpenTag = "<FONT COLOR="" + changeColor + ""
xmlns="http://www.w3.org/1999/xhtml">";
SPDiffUtility.ChangeCloseTag = "</FONT>";
SPDiffUtility.DeleteOpenTag = "<STRIKE XMLNS="HTTP://WWW.W3.ORG/1999/
XHTML"><FONT COLOR="" + deleteColor
+ "">";
SPDiffUtility.DeleteCloseTag = "</FONT></STRIKE>";
SPDiffUtility.InsertOpenTag = "<FONT COLOR="" + insertColor + ""
xmlns="http://www.w3.org/1999/xhtml">";
SPDiffUtility.InsertCloseTag = "</FONT>";

//Get the changes
string newValue = SPDiffUtility.Diff(e.OldValue, e.NewValue, 255);

Now you need to apply the new value to your form, which involves adding it to the repeating group. Add the code from Listing 5 to your method to facilitate this.

The NumberOfChanges gets set to the number of rows, the Changes field takes the changes from the SPDiffUtility, and the UserName gets set to the user making the changes.

Listing 5. Create a New Entry
//Create New Entry
XPathNavigator rTable = MainDataSource.CreateNavigator();
XPathNodeIterator tableRows = rTable.Select("/MY:MYFIELDS/
MY:GROUPTRACKCHANGES/MY:GROUPCHANGES",
NamespaceManager);
string rowNumber = Convert.ToString(tableRows.Count + 1);

using (XmlWriter writer = MainDataSource.CreateNavigator()
.SelectSingleNode("/MY:MYFIELDS/MY:GROUPTRACKCHANGES",
NamespaceManager).AppendChild())
{
writer.WriteStartElement("GROUPCHANGES", myNamespace);
writer.WriteElementString("NUMBEROFCHANGES", myNamespace,
rowNumber);
writer.WriteElementString("CHANGES", myNamespace, newValue);
writer.WriteElementString("USERNAME", myNamespace, userName);

writer.WriteEndElement();
writer.Close();
}

Tip

Make sure you write the elements in the same order as they appear in your repeating group.


Before you preview the form, you need to change the security of the form:

1.
Select File, Info.

2.
Click the Form Options button. The Form Options dialog appears.

3.
In the Form Options dialog, select Security and Trust.

4.
Change the security level to Full Trust, as shown in Figure 3.

Figure 3. Changing the security allows the code to execute in the form.

5.
Click OK.

Preview your form and add some text into the first text box. Click anywhere outside of the text box to trigger the change. The repeating section gets a new entry, as shown in Figure 4. The entry shows the HTML and not the rich text. The next section shows you how to apply rich text to the entry.

Figure 4. Adding text to the text box produces a new entry in the repeating section.
Other  
  •  InfoPath with SharePoint 2010 : Set Up the Form for Tracking Changes
  •  Sharepoint 2013 : Farm Management - Rename a Server on the Farm, Display the Configured Managed Paths
  •  Sharepoint 2013 : Farm Management - Retrieve the System Accounts, Retrieve Managed Accounts
  •  Sharepoint 2013 : Farm Management - Refresh Installed Products,Change the Port of Central Admin,Change the Farm Passphrase
  •  Sharepoint 2013 : Farm Management - Review Farm Configuration Values,Set the Farm Configuration Values
  •  Web User’s Best Buys March 2014
  •  Sharepoint 2013 : Using SkyDrive Pro - Discontinuing sync between SkyDrive Pro and your local device
  •  Sharepoint 2013 : Using SkyDrive Pro - Locating followed documents by using SkyDrive Pro, Synchronizing SkyDrive Pro to your local computer
  •  Sharepoint 2013 : Using SkyDrive Pro - Following SkyDrive Pro documents, Locating followed documents by using the newsfeed
  •  Sharepoint 2013 : Using SkyDrive Pro - Sharing SkyDrive Pro files
  •  
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us
    programming4us
     
     
    programming4us