How to Customise SharePoint List Forms in Visual Studio to Use One's Own Code

Posted by Sophina Dillard on Saturday 6 July 2013 0

SharePoint developers and consultants get this requirement from clients related to list forms all too often. A client needs the default list form fields but in a different order, and maybe with some minimal business logic. For example, let us assume there are 6 columns/fields in the list. For normal users in new form/edit forms all the fields will be in editable mode. But for managers suppose you don’t want field 4 and 5 to be enabled?

Going by the most widely used practices this can be achieved three different ways:

1.    Customising the form totally by creating custom web parts/user controls
2.    Using InfoPath forms and then handling the business logic with some custom JavaScript
3.    Using visual studio to replicate the default forms, then making minimal changes to the fields and then using server side code to handle the enable disable of fields.

Although I’ve talked about creating a custom New Item Form before, in this article we will concentrate on using visual studio (approach #3) specifically to achieve our slightly different goal.

To do so, we will start by replicating the default list forms in visual studio. I am not going to re-write the whole procedure of doing this step, however you can follow the steps in this article or this one if you need insight into how to accomplish this stage. Both of those cited posts provide very good learning scenarios for situations in which we do not need too much customization in default forms.

Now let’s take a scenario as described below and try to use these concepts to customise the form:

1.    Take the example of an appraisal form
2.    There are 3 fields for Developer to fill : Project Name, Score, Manager Name
3.    There is 2 fields for manager to fill : Manger’s score and Manager’s comments
4.    In edit form, when developer logs in First 3 fields will be editable, other 2 fields will be non-editable
5.    In same edit form, when manager logs in last 2 fields will be editable only.
So in this example we have four fields. We will use the concepts as described in the above links to generate the edit form.

Then we will put a panel around the three fields mentioned in #2 above and name it pnlUser; and we will put another panel around the two fields mentioned in #3 above and name it pnlManager.

Now we will write server side code in the edit form .cs file to enable and disable the panels based on the user logged in. The code in the edit form cs file will be something like this:

public partial class EditForm : WebPartPage
    {
        SPWeb web = null;
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            web = SPContext.Current.Web;
            SPUser managerFromList = null;
            try
            {
                if (!String.IsNullOrEmpty(this.Page.Request.QueryString["ID"]))
                {
                    pnlManager.Visible = false;
                    pnlUser.Visible = true;
                    id = Convert.ToInt32(this.Page.Request.QueryString["ID"]);
//Put your list name in the list name section
                    SPList list = web.Lists.TryGetList("List Name");
                    SPQuery query = new SPQuery();
                    query.Query = string.Format("<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>", id);
                    SPListItemCollection items = list.GetItems(query);
                    if (items.Count > 0)
                    {
                        SPListItem item = items[0];
                        if (null != item["Manager"])
                        {
                            if (item["Manager"].ToString().Trim().Length > 0)
                            {
                                web.AllowUnsafeUpdates = true;
                                string[] users = item["Manager"].ToString().Split(";#".ToCharArray());
                                managerFromList = web.EnsureUser(users[2]);                          
                                web.AllowUnsafeUpdates = false;
                               
                                if (managerFromList.Email.ToString().ToLower() == web.CurrentUser.Email.ToString().ToLower())
                                {
                                    pnlManager.Visible = true;
                                    pnlUser.Visible = false;
                                   
                                }                            
                            }
                           
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
        }     


       
    }
You can extend this code to achieve any level of customization you need and for all the forms (edit, view, new forms). I hope this helps! Happy coding!

Authored by:
Alvin Ubiera is a Microsoft SharePoint Technologist at the Houston-based business software provider, Rand Group and is a regular contributor to Dynamics 101. He hails from the sunny Dominican Republic and has varied experience in architecture, web development, and SharePoint.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

Related posts

0 comments:

back to top