Three years ago, I wrote a blog post called Displaying SharePoint Fields by Permission Level. This one has the highest amount of hits, but it is an imperfect solution. One of the most common questions that was asked was to see if there’s a way to show or hide fields in an ASPX form based on who the logged in user is, as opposed to according to permission level.
This is going to be a three part blog:
- Part 1: Using an ASPX form, with a SharePoint list of certain people who will be allowed to edit certain fields
- Part 2: Using an ASPX form, with a SharePoint group of people
- Part 3: Using an InfoPath form, with a SharePoint list
In this example, I have a task list, and there are only certain people who should be able to edit the task’s due date. Others should be able to see it but not edit it.
This solution in part 1 can be done with any version of SharePoint: MOSS, WSS 3.0, SharePoint 2010, and Foundation, and even SharePoint Online with Office 365.
- Create a custom list on your site, just to keep the list of people who are authorized to edit the certain fields in your form. I’ll call it “Task Admins”. Create a field called “Person”, as a people field. Add a few people to the list.
- Open SharePoint Designer. In Lists and Libraries, open your task list. In the Forms section on the right, click the New button.
- Call your new file EditCustom, pick Edit item form, and check the box to set as default. Click OK.
- Click the name of your new EditCustom.aspx form to open it.
- Click your cursor inside the form on the screen somewhere, so that the contextual ribbon will show the appropriate options. Click the Parameters button on the Options tab.
- Click the New Parameter button. Call it the TaskAdminParam, select Query String, and type TaskAdmin as the query string variable. Click OK. (side note: all those other 3 variables were already there, I didn’t add them)
- Select the Due Date field with your cursor:
- On the Options tab in the ribbon, click Conditional Formatting, and choose Show Content.
- On the Condition Criteria screen, click the Advanced button.
- Type $TaskAdminParam!=” then click OK and click OK again. This means that if the task admin parameter is not blank, it will show the content you selected, which is the date editable field.
- With the cursor still in the Due Date right column, select the Due Date field in the Data Source Details pane on the right. Click Insert Selected Fields As… choose Formatted… and choose DateTime.
- Now you’ll see a “read only” date on your form. It’s time to create a rule that will show this field when the logged in person is not a task admin. Note that with conditional formatting, you do have the ability to select an entire row if you want, and just show/hide the whole thing.
Select the new date you just added to the form. Click the Conditional Formatting button and choose Show Content. Repeat steps 9 and 10, except this time don’t put an exclamation mark (!) in the formula.
- Save your changes.
- Click the F12 button on the keyboard to open the page in the browser. Be sure to flip back over to SharePoint Designer and CLOSE that aspx form.
- In the browser, click the Page tab. Click the Edit Page button.
- Click the big white section on the page that says Add a web part. Add the Task Admins list that you created at step 1.
- For the Task Admins web part, in the Toolpane, click Edit the Current View. Scroll down to the Filters section, and set it so that Person is equal to [me]. Click OK.
- On the Page tab, click Edit Page again. Click the black arrow at the top right of the Task Admins web part, and choose Edit Web Part. Click the black arrow at the top right of the Task Admins web part, and choose Connections. Choose Send Row of Data to, and choose your Task web part.
- Choose Get Parameters From, and click Configure.
- The Provider field name is “Person”, and the consumer is the TaskAdminParam. Click Finish.
- Now that the web parts are connected, and you’re seeing the editable Due Date field if you’re in the list of admins, it’s time to hide the task admins web part. In the web part toolpane for Task Admins, expand the Layout section. Check the box next to Hidden, and click OK.
- On the page tab at the top, click Stop Editing.
Go ahead and test the solution. Navigate to your Tasks list as you normally would, and Edit one of the tasks. You’ll notice that if you’re in the list of task admins, you’ll see the Due Date as editable. Then, take yourself out of the list of Task Admins, and then when you edit a task, you won’t be able to change the due date.
Technorati Tags: SharePoint,data view web parts,web parts,parameters,forms,fields,list form