I have created a custom ListForm template and overide the ListFieldIterator.
I have for example 10 fields in my content type. 5 of these 10 fields must be shown for the creator of the item. The other 5 must be exluded. So I need to check if this is a new item and not an exiting item.
Step 2, we need to check if the item is existing, and the user is going to edit it, I would like to check if the user is member of an custom usergroup. If it is member of this usergroup, I would like to show all the fields and exlude 0 fields. Otherwise, if the user is not a member of the usergroup I would like to show all the 10 fields, but 5 of the 10 fields need to be readonly.
public class CustomListFieldIterator : ListFieldIterator
{
protected override bool IsFieldExcluded(SPField field)
{
if(this is an new item)
{
// show only 5 fields
}
else
{
// this is an existing item, you are editing this item
// check if the current user is member of an custom usergroup "Auditors"
SPWeb site = SPContext.Current.Web;
int groupId = site.Groups["Auditors"].ID;
if (site.IsCurrentUserMemberOfGroup(groupId))
{
// show all the fields
return false;
}
else
{
// show all the fields, but 5 of the 10 fields READONLY
}
}
}
}
I defined my custom contenttype as follow with a reference to my custom listForm template:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Parent ContentType: Item (0x01) -->
<ContentType ID="0x0100839f032740084ac2ae6ea6ab45928a65"
Name="MyCustomContentType"
Group="MyCustomContentType"
Description="MyCustomContentType"
Inherits="FALSE"
Version="0">
<FieldRefs>
<!-- These are 5 fields always visible-->
<!-- Everybody is enabled to edit these fields -->
<FieldRef ID="{EBC17A5D-F320-43D0-A275-7C657EA6F6C9}" Name="RFCDescription" ShowInEditForm="TRUE" ShowInNewForm="TRUE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{76BF7D5B-6A12-48D1-9827-ADE990B03216}" Name="RFCCompany" ShowInEditForm="TRUE" ShowInNewForm="TRUE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{9C9ED55A-7025-47F4-9637-27D5D2A1BCAE}" Name="RFCDepartment" ShowInEditForm="TRUE" ShowInNewForm="TRUE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{87DA58EB-3C70-4EF4-B833-1D4A93D60AC9}" Name="RFCClient" ShowInEditForm="TRUE" ShowInNewForm="TRUE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{56398822-7A4F-4155-A875-ADD09F32D4F2}" Name="RFCContact" ShowInEditForm="TRUE" ShowInNewForm="TRUE" ShowInDisplayForm="TRUE"/>
<!-- These are 5 fields only visible in edit mode -->
<!-- only users in usergroup "Auditors" may change these fields, otherwise readonly -->
<FieldRef ID="{1A64CE4A-716C-4572-B344-E7CCFE443D50}" Name="RFCScope" ShowInEditForm="TRUE" ShowInNewForm="FALSE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{39BC744E-08D4-410C-9D66-B728221634F5}" Name="RFCOperatingCosts" ShowInEditForm="TRUE" ShowInNewForm="FALSE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{09B7ED41-C5D0-4B8F-B102-F698164A05B9}" Name="RFCExpertise" ShowInEditForm="TRUE" ShowInNewForm="FALSE" ShowInDisplayForm="FALSE"/>
<FieldRef ID="{C95F5617-0C01-49F2-AA5C-8ADE757901F4}" Name="RFCPriority" ShowInEditForm="TRUE" ShowInNewForm="FALSE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{D6E32A83-D6FD-431B-B2CB-625516D3935F}" Name="RFCDatePlanned" ShowInEditForm="TRUE" ShowInNewForm="FALSE" ShowInDisplayForm="TRUE"/>
<FieldRef ID="{427E04A8-45E5-4243-BFE9-5FDF6FC6205B}" Name="RFCState" ShowInEditForm="TRUE" ShowInNewForm="FALSE" ShowInDisplayForm="TRUE"/>
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<Display>MyCustomListFormTemplate</Display>
<Edit>MyCustomListFormTemplate</Edit>
<New>MyCustomListFormTemplate</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>
I have implemted 5 fields will be visible in new and edit mode. The other 5 fields will be only visible in edit mode.