Many times we have a requirement such as: At the time of editing a sharepoint list item we need to add some validations based upon who has created the item during edit.
In this article we will see how to find the created by user of the item in the editform.aspx sharepoint list form.
There are many ways to get the Created by field of an item when it is opened in display mode or in edit mode.
We can directly find the control which is holding the value of the created by using jQuery or we can use an ECMAScript / client object model to get the created by.
Here we will see how we can fetch the created by field by directly accessing the control value using jQuery.
To do this, follow the steps given below:
Step 1: Open the site in SharePoint designer.
Step 2: Navigate to the list library and open the editform.aspx in advanced mode.
Step 3: Now search for the PlaceHolderAdditionalPageHead and add the link for jQuery:
<script src="../../Script%20Library/jquery-1.6.2.min.js" type="text/javascript"></script>
You can download the jQuery library from CodePlex.
The following is the jQuery used to find the Created By field.
var CreatedBy;
var isMozilla = $.browser.mozilla;
if (isMozilla)
{
CreatedBy = $("#onetidinfoblock1")[0].textContent.split('by')[1];
}
else
{
CreatedBy = $("#onetidinfoblock1")[0].innerText.split('by')[1];
}
Mozilla and IE havie different properties for this control. For Mozilla it is textContent and for Internet Explorer it is innerText.
In this way we can determine who has created the list item and based upon that we can add validations like enable or disable the controls.