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.
|
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.
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.
|
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.