Blog Archive

Friday, August 10, 2012

Adding a Context Menu to Different Column in List View

There are situations in which List View Web Part in SharePoint 2010 is very handy. When we insert the SharePoint 2010 List View Web Part on the page then  by default it will add certain columns of the list. Normally Title column has Context Menu for the List Item but what if we want to have that context Menu on some different column and here is our solution.

We can have Context Menu on some different column in the List View Web part. We can do it by using the small change in the FieldRef column in the list view web part. Add ListItemMenu and LinkToItem Attributes to the desired Field and set the value as 'True' for them.


<FieldRef Name="Name" ListItemMenu="TRUE" LinkToItem="TRUE" />

Clearing the Cache using Code

I come across a situation where I have to perform the manual Sign Out of the SharePoint 2010 Site and the easiest way to do so is just clearing the cache using code.

Following Code will remove all the cache files from your computer and will clear all the browser cache and indirectly forcefully sign out from the SharePoint site. (As we are clearing the FedAuth Cookie and its information in the Cache.)


     public void ClearApplicationCache()
    {
        List<string> keys = new List<string>();

        // retrieve application Cache enumerator
        IDictionaryEnumerator enumerator = Cache.GetEnumerator();

        // copy all keys that currently exist in Cache
        while (enumerator.MoveNext())
        {
            keys.Add(enumerator.Key.ToString());
        }

        // delete every key from cache
        for (int i = 0; i < keys.Count; i++)
        {
            Cache.Remove(keys[i]);
        }
    }

Opening the Pop up page in SharePoint and refresh the parent after closing the Pop-up.


SharePoint 2010 has lot of out of box JavaScript functions but they are not directly open. One of it and very useful function is "OpenPopUpPage" and this function is very handy in the development of SharePoint.

This function will open the new page in the Pop up window (ModalDialogBox) and when that is closed then the parent page will get refreshed. This is very similar to add new item in the list.


<a onclick="OpenPopUpPage('http://SharePointPageLink', RefreshPage); return false;" href="">This will open the page in Pop up and will refresh the parent page when the Pop Up page is closed. < /a>



Wednesday, May 23, 2012

Converting the Date Time into Date using XSLT

Most of the times when we are modifying web parts then we need to show the date in the MM/DD/YYYY format and in the list column is Date Time. Here we want to remove the Time field and show only the date that too specific format. 

In this case we can use the following XSLT expression to convert the Date Time into Date in MM/DD/YYYY format. Use the following syntax in your select attribute of xsl:value.

ddwrt:FormatDateTime(string(@StartDate),1033,'M/d/yyyy')

Here Start Date is the Date Time Field and it will show only date element in MM/DD/YYYY format.

Sunday, March 11, 2012

Removing Link from Lookup column in Data View WebPart

As I have started working more on Client Side SharePoint Programming, I come to know that lot of things are possible with Data View Webpart and they are very much powerful.

In most of the time when we are using DataView Webpart and if there is a Lookup column then by default it creates the link for it. Now if you want to remove this link there is a simple trick in the XSLT.

Please see the code below to remove this link. This is XSL value of tag and in select you can use the following XSLT syntax. Also use Disable Output Escaping attribute to Yes.

select="substring-before(substring-after(@FieldInternalName,'>'),'<')" 

Feel free to use this XSLT and it will save your lot of time!!!

Thursday, September 8, 2011

Fixing JavaScript Errors in SharePoint

Most of the times in IE8 you must have seen that while loading SharePoint site IE shows some weird JavaScript Errors and some UI functions in the Ribbon don't work. Recently I have observed this with one of the client and on some machines we faced this issue,

After lots of googling and trying different things I found the solution, so I want to share this solution with you if you are facing the same issue.

Well, when the SharePoint site loads in the browser then it shows the error like this.

Error: 'SP.Res.autocompleteLoading' is null or not an object
or
Error: 'SP.Res.lcid' is null or not an object

and the Solution to this problem is, in IIS find the directory "/_vti_bin/sts" that has an Application Pool assigned as "SecurityTokenServiceApplicationPool", which sometimes does not exist. Change this Application Pool to your Site Application Pool and save. Again reload the page and the error gets vanished.

If this does not work with you try this thing.
In web.config, make sure the following section exists:

<system.web.extensions>
  <scripting>
    <scriptresourcehandler enablecaching=”true” enablecompression=”false”>
  </scriptresourcehandler>
  </scripting>
</system.web.extensions>

First, make sure it’s not there already since that will generate errors. If it not there then put this section and recycle Application Pool and Check again...

Try if this works for you....

Wednesday, August 24, 2011

Hide the "View All Site Content" Link in SharePoint

To hide the link of "View All Site Content" in SharePoint, open the Master Page in SharePoint Designer and find the SPSecurityTrimmedControl element. This element contains a Div with class ms-quicklaunchheader.

The PermissionString attribute of SPSecurityTrimmedControl element determines which users can view this content. So by changing the value of this attribute to "ManageWeb", only Administrators can see this link. But by changing this value the master page becomes unghosted but this is the simplest trick to hide the link from all other users.

See this page for a list of all the possible permission string values.  

Monday, November 29, 2010

Enable Session State in SharePoint 2007

Here's a procedure to enable Session State within your MOSS 2007 farm.

Our session state will be stored in our SQL Server database. 

This is required over the traditional memory-based session because of multiple application servers in a web farm architecture.

Secondly, in order for our web applications to store session variables in SQL, we need to "prep" the database for session state data. 


 

1.  Create a Shared Services Provider.  This is required in order to enable Session State and its content database is the location of our session data.  If you already have an SSP, you can use the existing one.

2.  If needed, associate your web application with this SSP (done by default if it is your first SSP).

3.  In Central Administration, under Application Management, choose Session State, and ensure that it is enabled.

4.  From a command prompt, navigate to the Microsoft.NET 2.0 Framework directory

(Typically:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727)

5.  Modify your web.config:

Uncomment the following line: 

<add name="Session" type="System.Web.SessionState.SessionStateModule" />

Modify the following line:

<pages enableSessionState="true" … />

6.  Run the following command:

aspnet_regsql.exe -S <server_name> -d <database_name> -ssadd -sstype c -E

 
 

Where:

<server_name>  is the database server where your database is located

<database_name>  is the content database of your shared services provider


 

Run aspnet_regsql.exe /?  for more information on the remaining options

How to Find out the Installation is MOSS 2007 or WSS 3.0

Here is the code which identifies the Installation is MOSS 2007 or WSS 3.0

/// <summary> 
/// Method to find out if a SharePoint installation is MOSS or WSS 3.0 
/// </summary> 
public static bool IsMOSS() 
{ 

   SPFeatureDefinitionCollection features =  SPContext.Current.Site.WebApplication.Farm.FeatureDefinitions; 
    if (features["OssNavigation"] != null && features["Publishing"] != null) 
        return true; 
    else 
        return false; 
}

Tuesday, November 23, 2010

Adding a SPUser to SharePoint Group

For adding a SharePoint user to a particular SharePoint group
following code can be useful.



SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteURL))
    {
      using (SPWeb web = site.OpenWeb())
      {
        Microsoft.SharePoint.Administration.SPWebApplication webApp = 
        web.Site.WebApplication;
        webApp.FormDigestSettings.Enabled = false;

        web.AllowUnsafeUpdates = true;
       //Assign role and add user to site
       SPGroupCollection groupColl = web.SiteGroups;
       if (GroupExists(groupColl, GroupName))
       {
         //Add the user to a group.
         SPGroup roleGroup = web.SiteGroups[GroupName];
         using (SPSite newSite = new SPSite(baseSiteURL))
         {
           using (SPWeb newWeb = newSite.OpenWeb())
           {
               reply = addUserToGroup(newWeb, roleGroup,
               userName, firstName, lastName, userEmailId);
            }
         }
       }
       else
       {
         reply = false;
       }
       web.Update();
       webApp.FormDigestSettings.Enabled = true;
    }
  }
 });

Another private method we have to write here.

private bool addUserToGroup(SPWeb web, SPGroup group, 
string userName, string FirstName, string LastName, string userEmail)
{
  bool updateFlag = false;
  try
  {
    //Make Allow unsafe updates true so that security problem should not come.
    web.AllowUnsafeUpdates = true;
    group.AddUser(userName, userEmail, FirstName, "Automatically Added User");
    group.Update();
    web.Update();
    updateFlag = true;
  }
  catch (Exception ex)
  {
     updateFlag = false; 
     throw ex;
  }
}