SharePoint 2010 Tips and Tricks with CSS

While working with SharePoint projects I am sure you all would have been asked by the customers for implementing something which you would at the first place say “no” or “not possible”. But as they say the customer is always right and you need to give in to customers demand. Following are few things which I needed to implementation while working on a recent intranet portal which I would like to share with you so that it helps somebody out there.

  1. Edit the Page where you need to apply the styles
  2. Add a “Content Editor Web part” on the page and make it hidden
  3. Add the following to the content editor web part if you need to apply all the four customizations mentioned below OR pick and choose according to your requirement
<style>
/*--------Hide the columns--------*/
.ms-viewheadertr {
    display: none;
}
/*---------Hide the Add Document option-------*/
.ms-addnew {
    display: none;
}
/*---------Remove the new image icon on newly added documents-------*/
IMG.ms-newgif {
    display:none;
}
/*---------Hide the web part selection checkbox------*/
.ms-WPHeaderTdSelection
{
display: none;
}
</style>

Note: If you need to apply these settings to complete site then you should add it to a css document and reference that document in your master page 
Hide the “New” icon on newly added documents in document library
Before:
clip_image00d2
After:

clip_image00d4
Hide the “Add document” link on a SharePoint document library
Before:
clip_image00d6
After:

clip_image00d8
Hide the “Columns” in SharePoint document library
Before:
clip_image01d0
After:
clip_image01d2
Hide the “selection” checkbox
Before:
clip_image01d4
After:

clip_image01d6

Show custom error message instead of Web part access denied – SharePoint 2010

Many times while using “ListViewWebPart” you get the following error if the user does not have access to the list/library
Error Web Part Error: Access denied. You do not have permission to perform this action or access this resource”
clip_image00f2
Though the error is kind of explanatory but is not very user friendly. I have getting demands from lot of customers to replace this error with a custom error message. Let us see how we can achieve this using Jquery
1. Download latest version of Jquery
2. Edit Page where you have added you ListViewWebPart and add a “Content Editor Web part” at the end of the page. Make the web part hidden
3. Add the following JavaScript to the “Content Editor Web Part”

<script type="text/javascript" src="/Shared%20Document/jquery.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('div[errorwebpart="1"]').text('The content of this section is restricted to authorized users only.');
$('div[errorwebpart="1"]').addClass("ms-vb2");
}
);
</script>

Note: the path for the Jquery should be your path for the Jquery file

Once done you will be able to see the custom message instead of generic Sharepoint message

clip_image00v4

How to get Site Column Id in SharePoint without writing code (using UI)

Many a times we need site column id to be used somewhere or the other and for that we might require to get it reasonably quickly and moreover without having to write any code to fetch it. I am going to show you how you can do that, let’s begin Step1: Navigate to Site Settings of your SharePoint Site clip_image00b2 Step2: Select “Site Content Types” from the Galleries group. It will show you list of content types as below clip_image00n4 Step3: Select the content type which contains your site column. It will display the settings page for the content type as below clip_image00a6 Step4: Select the column for which you need to get the ID. It will take you to the column settings page as shown below imagebacground Step5: While on this page notice the URL in your browser, which will be similar to below http://yoursiteurl/_layouts/ManageContentTypeField.aspx?ctype=0x010105&Field=UIVersion&Fid={8e334549-c2bd-4110-9f61-672971be6504} Notice that the Fid at the end of the URL is your site column Id Important note:
In your browser URL, this string may be encoded, so what you see is this:
Fid=%7B8e334549%2Dc2bd%2D4110%2D9f61%2D672971be6504%7D Explanation of the encoding: %7B = “{“ %2D = “-“ %7D = “}” Hope this helps somebody out there

Extending the SharePoint Security Trimmed Control to support SharePoint Groups

We all know that (SPSecurityTrimmedControl) SharePoint Security Trimmed Control comes to our rescue when we need to show role/permission based data in SharePoint. It helps you Conditionally renders the contents of the control to the current user only if the current user has permissions defined in the PermissionString. <Sharepoint:SPSecurityTrimmedControl runat=”server” Permissions=”ManageLists”>
      Place your control(s) here
</SharePoint:SPSecurityTrimmedControl> All this is good and works fine for most of the scenarios, however you may seldom find a need to be able to add a “SharePoint Group” instead of a “PermissionString” So, let’s tell you how… The idea is to create your wrapper control to support this feature. Let’s get started Create a custom Web Control and write down the required logic based on your requirements. This should inherit from SPSecurityTrimmedControl

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web;
using Microsoft.SharePoint;

namespace SPSecurityTrimmedControlExtender
{
public class SPUserGroupTrimmedControl : SPSecurityTrimmedControl
{
private List groups = new List();

public string GroupsString
{
get
{
return string.Join(",", groups.ToArray());
}
set
{
groups.AddRange(
value.Split(new char[] { ',' },
System.StringSplitOptions.RemoveEmptyEntries)
);
}
}

protected override void Render(HtmlTextWriter output)
{
if (!string.IsNullOrEmpty(GroupsString) && IsMember())
{
base.Render(output);
}
}

private bool IsMember()
{
using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())
{
bool isMember = false;
foreach (string group in groups)
{
isMember = web.IsCurrentUserMemberOfGroup(web.Groups[group].ID);
}
return isMember;
}
}
}
}

Now, put this assembly in GAC and add a safe control entry in the web.config of you web application.


You are now ready to use this in your web page or master page by registering it.



//your control to show to the specified group only

And you are done!!!

Error: Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

You may have noticed this error while doing SharePoint development. I also faced it the other day hence thought of penning it down so that someone else could benefit from this
You typically get this error when you unknowingly try to dispose the SharePoint Objects. We try to perform the SharePoint best practices and this sometime may lead to few nervy moments during development.
For e.g.
The following code will give you the above mentioned error “Trying to use an SPWeb object that has been closed or disposed and is no longer valid

using(SPWeb web = SPContext.Current.Web)
{
   //your code
}

At the first look nothing seems wrong with the above code, but the real culprit is the using statement above as it automatically disposes the SPWeb object. SPContext allows you to reference to the object SharePoint has created and hence should not be disposed as it will destabilize SharePoint operations.

So the moral of the story is to do not dispose the SPContext objects in your code.

SPWeb web = SPContext.Current.Web