Hide or disable a field in sharepoint Editform.aspx base on the role?
Hi All,

I create a custom column in the form, I would like to only show it to the user who has the Full control in the edit form.
For the user with other role, I would like to hide it. Is there any way I can do this thru the list setting without code.
Thank you for any help.

Ddee
July 13th, 2009 11:53pm


> Is there any way I can do this thru the list setting

No


> Is there any way I can do this thru the list settingwithout code

Depends on your definition of code. You can do it using SharePoint Designer.


The following is "at your own risk". (works for me though) Try it in a test site first.


Basic steps are to remove the ListFormWebPart and replace it with a DataFormWebPart (Custom List Form) and wrap the items you want to hide in a SPSecurityTrimmedControl.

Here are the steps:

Open the site in SharePoint Designer
Open the Lists folder and find your list and open it
Open Editform.aspx (best practice is to create a new form page...)
Click the ListFormWebPart and then edit the Web Part propertites and Hide this web part.
Place the insertion point before existing web part and click Insert, SharePoint Controls, Custom List Form
Pick your list, content type and click Edit Item Form
(now you have an edit form that can be customized)

To control who can see part of a page you can use the SPSecurityTrimmedControl. It looks like this...

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">
... stuff you only want users with the ManageWeb permission ...
</Sharepoint:SPSecurityTrimmedControl>

A list of the values for the PermissionsString
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spbasepermissions.aspx

Display the source view of the page and find the table row for the field you want to hide.
Wrap everthing from <tr> to </tr> in the SPSecurityTrimmedControl (be careful with the capitalization!)

The result will be something like this:

<SharePoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">
	<tr>
		<td width="190px" valign="top" class="ms-formlabel">
			<H3 class="ms-standardheader">
			<nobr><em>Priority</em></nobr>
			</H3>
		</td>
		<td width="400px" valign="top" class="ms-formbody">
		<SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="Edit" FieldName="Priority" __designer:bind="{ddwrt:DataBind('u',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Priority')}"/>
		<SharePoint:FieldDescription runat="server" id="ff2description{$Pos}" FieldName="Priority" ControlMode="Edit"/>
		</td>
	</tr>
</SharePoint:SPSecurityTrimmedControl>								

Now for a bug... SharePoint Designer will show an error in the Design view pane:

"PriorityError Rendering Control - Unnamed2Type 'Microsoft.SharePoint.WebControls.FormField' does not have a public property named 'xat-id'."

Ignore this and save the page. Now go and edit one of the items as a full control user and again as another user.

Free Windows Admin Tool Kit Click here and download it now
July 14th, 2009 1:15am

Also take a look at this as a possible solution using Audiences.
http://sharepoint07.wordpress.com/2008/02/05/customize-the-newformaspx/
July 14th, 2009 1:25am

Hi,

You can achieve this kind functionality using javascript. Try to find current context and the on the basis of users rights you can show/hide fields.

Thanks
Free Windows Admin Tool Kit Click here and download it now
July 14th, 2009 10:56am

You could also look at a number of solutions available through Codeplex:

http://splistdisplaysetting.codeplex.com/

http://officetoolbox.codeplex.com/(I used this one in a number of projects)

November 11th, 2009 5:15am

Yes, this can all be done in JavaScript.  Just grab the person's name from the page with jQuery.  This is for SP 2013, but it has a similar label in 2010 and 2007:

var userName = $('a[id="zz5_Menu"]').text();

Then there is a web service call you could make against SP's built-in web service UserGroups.asmx to determine if the person is a member of the group you are looking for.  A method of
UserGroup.GetGroupCollectionFromUser is probably what you'd need - finding the groups for a given user, and parsing the returned XML to see if the group you're looking for is one of them.

Then you can use an "if... then" to hide a row or show a row based on if it is an Edit form, New form, or Display form:

HideFormRow('My Field', 'edit');

function HideFormRow(fieldDisplayName, newEditOrDisp) {
    // hide table row of a field
    try {
        var _spTrElement;
        if (newEditOrDisp == "new" || newEditOrDisp == "edit") {
            _spTrElement = GetNewEditFormRow(fieldDisplayName);
        }
        else
            _spTrElement = GetDispFormRow(fieldDisplayName);
        _spTrElement.style.display = 'none';
        return _spTrElement;
    }
    catch (ex) {
        throw new Error('An unhandled exception occurred while attempting to hide a form row.');
    }
}

function GetNewEditFormRow(fieldDisplayName) {
    try {
        var _result;
        var _spFormLabelTdElements = GetElementsByAttribute('TD', 'className', 'ms-formlabel');

        for (var _i = 0; _i < _spFormLabelTdElements.length; _i++) {
            if (_spFormLabelTdElements[_i].childNodes.length > 0) {
                var elemText = "";
                var elem = _spFormLabelTdElements[_i].innerText;
                if (elem.indexOf('*') != -1) {
                    var arrElem = elem.split('*');
                    elemText = arrElem[0].trim();
                } else {
                    elemText = elem.trim();
                }
                if (elemText == fieldDisplayName) {
                    _result = GetParentElementByTagName(_spFormLabelTdElements[_i], 'TR');
                    break;
                }
            }
        }
        return _result;
    }
    catch (ex) {
        return;
    }
}

function GetParentElementByTagName(targetObject, tagName) {
    try {
        var _result = targetObject;
        while (_result.tagName != tagName) {
            _result = _result.parentNode;
        }
        return _result;
    }
    catch (ex) {
        return undefined;
    }
}

function GetElementsByAttribute(tagName, attributeName, attributeValue) {
    try {
        var _result = new Array();
        var _allElements = document.getElementsByTagName(tagName);

        for (var _i = 0; _i < _allElements.length; _i++) {
            if (_allElements[_i].getAttribute(attributeName) == attributeValue) {
                _result.push(_allElements[_i]);
            }
            // This is to provide compatibility with SP 2013, which will find "class", but not "className"
            else if (_allElements[_i].getAttribute(attributeName) == null && attributeName == 'className') {
                if (_allElements[_i].getAttribute('class') == attributeValue) {
                    _result.push(_allElements[_i]);
                }
            }
        }
        return _result;
    }
    catch (ex) {
        return new Array();
    }
}


Free Windows Admin Tool Kit Click here and download it now
July 9th, 2015 3:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics