Second Life of a Hungarian SharePoint Geek

October 10, 2009

Changing system properties for an item from the SharePoint user interface – part 2

Filed under: Custom fields, SharePoint — Tags: , — Peter Holpar @ 03:40

UPDATE: See the source code of this post on CodePlex.

In the previous part you could read the introduction and the rather generic part of the custom field code.

The important code is in the control class. In the class constructor I store the contexts in member variables, and since I would like to implement some kind of security in this field, I also check if the current user is an admin on the site.

In the first part of the CreateChildControls method I’ve check the display mode and if it is not Edit or the user is not an admin, then I hide the field control, and there is no more things to do.

In the second part of the CreateChildControls method I look up the child DateTimeControl and PeopleEditor controls of the field and set their content based on the values stored for the current list item.

In the OnInit method I hook up an event handler to handle item save events if we are in edit mode.

The last part, the save event handler method called in this case SaveHandler is rather straightforward again. In this method I apply the technique described in Steven’s post, just using the values specified in the field child controls. Again, we modify the values only in edit mode and only when the user is a site admin.

   1: using System;

   2: using System.IO;

   3: using System.Security.Permissions;

   4: using System.Text;

   5: using System.Xml;

   6: using System.Web;

   7: using System.Web.UI;

   8: using System.Web.UI.WebControls;

   9: using System.Web.UI.HtmlControls;

  10: using Microsoft.SharePoint;

  11: using Microsoft.SharePoint.WebControls;

  12: using System.ComponentModel;

  13: using System.Diagnostics;

  14: using Grepton.SharePoint.FieldTypes;

  15:  

  16: namespace Grepton.SharePoint.FieldTypes

  17: {

  18:  

  19:     //Defines the field controls for the page

  20:     public class CorrectorFieldControl : BaseFieldControl

  21:     {

  22:         private DateTimeControl _createdDate;

  23:         private DateTimeControl _modifiedDate;

  24:         private PeopleEditor _createdBy;

  25:         private PeopleEditor _modifiedBy;

  26:         private SPContext _context;

  27:         private SPFormContext _formContext;

  28:         private bool _isAdmin;

  29:  

  30:         public CorrectorFieldControl()

  31:         {

  32:             _context = SPContext.Current;

  33:             _formContext = _context.FormContext;

  34:             _isAdmin = Web.CurrentUser.IsSiteAdmin;

  35:  

  36:         }

  37:  

  38:         //Returns the ASCX template to use

  39:         protected override string DefaultTemplateName

  40:         {

  41:             get { return "CorrectorField"; }

  42:         }

  43:  

  44:         protected override void CreateChildControls()

  45:         {

  46:             try

  47:             {

  48:  

  49:                 base.CreateChildControls();

  50:  

  51:                 if ((!_isAdmin) || (ControlMode != SPControlMode.Edit))

  52:                 {

  53:                     Visible = false;

  54:                     return;

  55:                 }

  56:  

  57:  

  58:                 _createdDate = (DateTimeControl)TemplateContainer.FindControl("CreatedDate");

  59:                 _modifiedDate = (DateTimeControl)TemplateContainer.FindControl("ModifiedDate");

  60:                 _createdBy = (PeopleEditor)TemplateContainer.FindControl("CreatedBy");

  61:                 _modifiedBy  = (PeopleEditor)TemplateContainer.FindControl("ModifiedBy");

  62:  

  63:                 _createdDate.SelectedDate = DateTime.Parse(ListItem["Created"].ToString());

  64:                 _modifiedDate.SelectedDate = DateTime.Parse(ListItem["Modified"].ToString());

  65:                 SPFieldUserValue createdBy = new SPFieldUserValue(Web, ListItem["Created By"].ToString());

  66:                 _createdBy.CommaSeparatedAccounts = createdBy.User.LoginName;

  67:                 _createdBy.Validate();

  68:                 SPFieldUserValue modifiedBy = new SPFieldUserValue(Web, ListItem["Modified By"].ToString());

  69:                 _modifiedBy.CommaSeparatedAccounts = modifiedBy.User.LoginName;

  70:                 _modifiedBy.Validate();

  71:  

  72:                 if ((_createdDate == null) || (_modifiedDate == null) || (_createdBy == null) || (_modifiedBy == null))

  73:                 {

  74:                     throw new ArgumentException("Corrupted CorrectorField control template");

  75:                 }

  76:  

  77:             }

  78:             catch (Exception ex)

  79:             {

  80:                 Trace.TraceInformation("Exception: {0}", ex.Message);

  81:             }

  82:         }

  83:  

  84:         protected override void OnInit(EventArgs e)

  85:         {

  86:             base.OnInit(e);

  87:             // add save handler

  88:             if (ControlMode == SPControlMode.Edit)

  89:                  _formContext.OnSaveHandler += new EventHandler(SaveHandler);

  90:         }

  91:  

  92:         protected void SaveHandler(object sender, EventArgs e)

  93:         {

  94:             if ((!_isAdmin) || (ControlMode != SPControlMode.Edit))

  95:                 return;

  96:  

  97:             if (_createdBy.Accounts.Count == 1)

  98:             {

  99:                 ListItem["Created By"] = Web.EnsureUser((string)_createdBy.Accounts[0]).ID;

 100:             }

 101:             if (_modifiedBy.Accounts.Count == 1)

 102:             {

 103:                 ListItem["Modified By"] = Web.EnsureUser((string)_modifiedBy.Accounts[0]).ID;

 104:             }

 105:             if (!_createdDate.IsDateEmpty)

 106:             {

 107:                 ListItem["Created"] = _createdDate.SelectedDate;

 108:             }

 109:             if (!_modifiedDate.IsDateEmpty)

 110:             {

 111:                 ListItem["Modified"] = _modifiedDate.SelectedDate;

 112:             }

 113:             ListItem.UpdateOverwriteVersion();

 114:             

 115:         }

 116:     }

 117:  

 118: }


 
I plan to publish this solution on CodePlex later either individually or as part of an existing package. Until that if you would like to use the code, feel free to copy that from this page, but don’t forget to change the public key token to match the one in your project, deploy the assembly into GAC, and add the control as SafeControl in the web.config of your site.

It is important to note, that the solution is provided “AS IS”, meaning not fully tested, data loss or side effects might be possible, and no support is guaranteed, etc. By using the code above you explicitly agree with this conditions. It might be also dangerous for your business to include this control on your site and allow users to change system properties. Just mention that for you to not say later I have not forewarned you about that.

Advertisements

1 Comment »

  1. […] Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Changing system properties for an item from the SharePoint user interface – part 2 Custom field types as an alternative way for list item event handling in SharePoint […]

    Pingback by Changing system properties for an item from the SharePoint user interface – part 1 « Second Life of a Hungarian SharePoint Geek — December 3, 2009 @ 02:36


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: