I am working on an internal instance of SharePoint 2010 and simply hiding the data on the forms using CSS is sufficient because the user would have to look in the source of the page to see the data.
Note: This is NOT a true security solution! All data (even for hidden columns) is sent to the browser, but is just hidden from user's view in the browser.
In my case, it would not be the end of the world if users viewed source to see all the data. We are all part of the same company after all. If this is your scenario, then you may want to consider the solution outlined below.
To implement the functionality to get user and group information, I wrote the following methods.
You can download the source here.
function isCurrentUserInSharePointGroup(sharePointGroupID, callbackFunc)
function isUserInSharePointGroup(userLogin, sharePointGroupID, callbackFunc)
CSOM uses asynchronous calls for everything which makes it difficult to code if you are not accustomed to it. The good news is all you have to do is provide the callback function to make use of these functions. While that may sound difficult, just follow the example I have provided. In the example code the first thing it does is make sure the CSOM library (sp.js) is loaded and then executes our code. It takes the approach of getting the current user then seeing if the user is in the owner group and setting column visibility appropriately. If the user is not in the owner group it then looks in the members group and sets the column visibility appropriately. If they are not in either of those groups it sets the column visibility appropriately. You could continue on with different checks if desired.
Here is a snippet of what the solution code looks like:
function doOwnerChanges(userLogin, isInGroup)
else // not in owner group let's see if they are in members group
isUserInSharePointGroup(userLogin, 7, Function.createDelegate(this, this.doMemberChanges));
As you can see it is easy to maintain because all the complexity is abstracted away. If another column is added you would just add a line to the above code.
Added solution to your page
- Download the following:
- SPUserGroupUtility.js My code that gets the current user and checks the groups the user is in.
- ShowHideColumnExample.js (only needed if you want an example of how to use this solution). You can replace or not use this in your specific implementation. This is the file you will need to specify your columns. The name is not important except that you reference it later.
- SPUtility.js This provides the functionality for hide/showing fields and making them readonly, etc.
- Prototype.js Required by SPUtility.js.
- sp.js - actually you don't need to download it. Ultimately, this is the CSOM library. It is provided by SharePoint 2010. Just use it in your code as needed.
- Upload the .js file to somewhere on SharePoint where all users can access the files. I recommend installing your .js file in your Site Assets directory on your site. You can find it by going to Site Action | View All Site Content | Site Assets.
- Navigate to form (EditForm.aspx or NewForm.aspx)
In SharePoint 2010, you can choose Form Web Parts -> Default whatever Form
- Add a Content Editor Web Part to the page
- Edit the web part
In SharePoint 2010, click the arrow -> Edit Web Part.
In SharePoint 2010, under Editing Tools -> Format Text click the HTML button -> Edit HTML Source
- Follow the instructions on installing the SPUtility.js
- Save the page / stop editing.
- NOTE: To just try SPUtility.js you can try the install instructions.