Conditionally show or hide fields within a custom list form based on user permissions

InfoPath, I really like InfoPath. I like the interface and how easy it makes editing custom list forms in SharePoint (especially if you want to make snazzy looking forms in SharePoint 2010). However, custom actions do not like InfoPath, not one bit.

The problem

Here’s the situation, we have a heavily customised custom SharePoint 2010 list which was leveraging InfoPath based forms. We were getting reports of latency when trying to open the InfoPath forms and the decision was made to revert back to the default ones.

Also, another requirement was to have a two edit forms for this list, one set as default with several fields omitted for end users – another one for administrators of the list. Once the edit forms were created I embarked on my journey of creating a custom action that would open the administrator’s edit form from the ribbon, utilising rights masks to make it only appear for those with adequate permissions.

I quickly found that this wasn’t going to work. I tried several times to create the custom action but it just wouldn’t appear. Even after reverting the list back to the default forms from InfoPath through list settings and deleting the InfoPath forms from server the custom action wouldn’t show. I found some useful conversations about this issue below:

After I realised the custom action approach wasn’t going to work, I made the decision to go down the route of having one custom edit form that would conditionally show or hide fields based on their permissions – the answer I found was within a XSLT conditional if test!

The solution

Here are the steps taken to hide a field based on a user’s permissions within a custom edit form:

  • Open the list you want to edit in SharePoint Designer
    SharePoint Designer homepage
  • In the Forms section, open your custom edit form
  • Switch to Advanced Mode
    advanced mode sharepoint 2010
  • Use the design view and select the field you wish to hide
    split view sharepoint 2010
  • In the code view, add the following code snippet above and below your field:

<xsl:if test="ddwrt:IfHasRights(2048)">
</xsl:if>

  • The end result should look something like this:
  • Save the changes

If this has worked, you should now be able to test the edit form as a user with the correct permissions and see the field, then verify that for a user without the relevant permissions it’s hidden.

Within the if test, the number corresponds to a permissions mask that assigns a particular value (i.e. 2048 = Manage Lists).

Here is a list of all the values and permissions masks:

PermissionPermission Mask
ViewListItems1
AddListItems2
EditListItems4
DeleteListItems8
ApproveItems16
OpenItems32
ViewVersions64
DeleteVersions128
CancelCheckout256
PersonalViews512
ManageLists2048
ViewFormPages4096
Open65536
ViewPages131072
AddAndCustomizePages262144
AppleThemeBorder524288
ApplyStyleSheets1048576
ViewUsageData2097152
CreateSSCSite4194314
ManageSubwebs8388608
CreateGroups16777216
ManagePermissions33554432
BrowseDirectories67108864
BrowseUserInfo134217728
AddDelPrivateWebParts268435456
UpdatePersonalWebParts536870912
ManageWeb1073741824
UseRemoteAPIs137438953472
ManageAlerts274877906944
CreateAlerts549755813888
EditMyUserInfo1099511627776
EnumeratePermissions4611686018427387904
FullMask9223372036854775807
Advertisement

The SharePoint date format problem

I know what you’re thinking, what date format problem? Well this is something that’s cropped up for me time and time again so let me explain. Most requests I get for new SharePoint lists will usually contain a variety of custom date columns and said list will also require that the default display form shows a unique view of this data.

On the default display form, you will notice that the custom date columns will be showing data that looks like this:

bad date format

Well this just won’t do, will it? The good news is we can fix it!

To be able to customise the display form, you will need to be able to connect to your SharePoint environment using SharePoint Designer, if you don’t have it you can download it from Microsoft here:

SharePoint Designer 2010 (32 bit)

For this example, we are only focusing on one custom date column, at the end of the post I will summarise how to fix this issue for multiple custom date columns.

  • Open SharePoint Designer and connect to your environment
  • Navigate to the lists and libraries and open the list you want to change
  • Within the Forms section – New Form
SharePoint New Form

(I’d recommend creating a new display form for no other reason than to avoid potentially breaking the active one).

  • Give the form a logical name so you can identify it at a glance as a display form (I usually go for DispForm2)
  • Press OK
  • The code behind the display form will open, don’t worry too much about this we are only interested in a small portion of the code.
  • In the Ribbon – under the Home Tab – Press Advanced Mode
Advanced button on ribbon
  • If it’s not already, at the bottom of the form – change the view to split view
SharePoint split view
  • Scroll through the design view of your form until you find the custom date field – select it
  • In the code view, make sure the following line is highlighted:
    <xsl:value-of select="@Start_x0020_Date"/>
    (NOTE: the bits after the @ will be the name of your date field)
  • If you amend this line of code to look like this:
    <xsl:value-of select="ddwrt:FormatDate(string(@Start_x0020_Date), 2057, 3)"/>
  • you should see something like this:
    11 June 2014
  • Save the display form
  • Press Preview in browser to double check the format date function is working as expected
  • Close preview browser, close the display form
  • In the forms section of the list – select your form
  • In the Ribbon – Set as Default
set as default

That’s all there is too it! It might seem like quite a few steps but it isn’t too bad once you get comfortable with the forms code view.

If you have more than one custom date column that you need to format, just amend the code of each custom date column using the example above as a guide (just make sure if you copy/paste the code that each @ name is different.

FormatDate function and locales explained

What our updated code is doing is inserting a FormatDate function, which allows us to add the locale parameters to the end of our line code (the 2057, 3). The locale parameters control what the output of the FormatDate function will be, here are some examples of the outputs and locale parameters:

OutputLocaleFormat
3/23/200910331
3/23/2009 12:00 AM10332
Monday, March 23 200910333
12:00 AM10334
Monday, March 23, 2009 12:00 AM10337
3/23/2009 12:00:00 AM103313
Monday, March 23, 2009 12:00:00 AM103315
23/03/200920571
3/23/2009 12:00 AM20572
23 March 200920573
00:0020574
23/03/2009 00:0020575
23 March 2009 00:0020577
00:00:00205712
23/03/2009 00:00:00205713
23 March 2009 00:00:00205715

If you are looking for a list of all the available locales you can find them here:

https://msdn.microsoft.com/en-us/library/ms912047(v=winembedded.10).aspx

That’s it for now, if you’ve got anything you’d like me to cover feel free to get in touch or leave a comment!

Hiding NEW! from items in lists or libraries

This is my first ever blog post…scary! I’ll start with a really easy solution to a problem I was tasked with solving by one of our departments…

Ever wanted to completely remove any reference to the pesky NEW! icon from freshly uploaded documents or list items? Yeah me either…well if in case you ever felt so inclined to do so here are some very simple steps to remove the icons from any new documents or items on a page.

This example is a SharePoint 2010 web application with a standard publishing site collection active:

    1. Navigate to the page you want to remove the NEW! icons from and begin editing
    2. Add a content editor webpart to the page; I added it to the bottom of the page but you can add it whenever suites
    3. Click on the down arrow to open the webpart menu – Select Edit Web Part
    4. You’ll now notice the content editor webpart has changed, it will now say ‘Click here to add new content’, click here!
    5. In the ribbon – Editing Tools menu – Format Text tab – Press HTML – Edit HTML Source
    6. In the HTML editor, copy and paste this little bit of CSS:
      <style>
      IMG.ms-newgif {display:none;}
      </style>
      
    7. Press OK on the HTML editor

    Voila! The NEW! icons have vanished and we can move on with our lives…the next little bit is totally optional, but I think for completeness it makes sense to do. We’re just going to rename and hide the content editor webpart so that it’s not visible and any editors know not to touch it:

    1. Click on the down arrow to open the webpart menu – Select Edit Web Part
    2. In the content editor webpart menu – expand Appearance
    3. Change the Title to ‘Do not delete’
    4. Change the Chrome Type to None
    5. Press Apply and OK

    That is all there is to it, we have successfully removed the NEW! icons so they are no longer visible and also hidden the webpart which contains the tiny snippet of CSS that makes the change.