Category Archives: Technology

Enable custom ribbon button only if user has permission to edit selected item

The simplest and way to hiding a SharePoint Custom action in a ribbon(/or anywhere) for users whom doesn’t have item edit permissions is, using “Rights” attribute of “CustomAction” element.  Comma separated set of rights can be specified here so that the cumulative rights set will be formulated by ANDing all different individual rights. All possible Rights available are SPBasePermissions

E.g:

<CustomAction
Id=”Ribbon.MyCustomAction”
Location=”CommandUI.Ribbon”
Title=”MyCustomAction”
RegistrationType=”List”
RegistrationId=”10001″ Sequence=”20″
Rights=”EditListItems,AddListItems” >

Advertisements

“Handle with Care “ – SharePoint Lists with Large lists

Here are few key things to remember when implementing SharePoint applications with high volumes of data.

    1. Never try to override List View Threshold. Keep it to 5000 as it is by default.
    2. Try avoiding increase of “List View Lookup Threshold”, keep it as 8
    3. Apply indexes to relevant columns. Be mindful about the content of column before adding indexes
    4. Use out of the box webparts as much as possible. Utilize parametrized list view webparts, connected webparts etc to achieve advance filtered listings
    5. Make sure to use indexed columns as much as possible in CAML queries
    6. Make sure to prioritized CAML query filters depending up on number of items filter out by each clause
    7. Avoid using People fields for CAML query filtering (anyway those are not index-able)
    8. Don’t rely on lookup columns heavily.
    9. Make sure you filter only the required set of list items
    10. Try using SPListItem.GetItemById whenever an Items being retrieved
    11. Avoid using SPQuery to make queries, instead user ContentIterator so that there will be less locks in DB
    12. If required, use caching(possibly ASP.Net Cache) when custom code is being used
    13. Off load processing from server as much as possible

 

      Some more tips by Amit Phatak at http://navigatingsharepoint.blogspot.com/2010/08/sharepoint-2010-handling-large-splists.html

Update Business Data Catalog Throttle Config with PowerShell

function UpdateBusinessDataCatalogThrottleConfig($maximum, $defaultVal)
{
    Write-Host "Update Business Data Catalog Throttle Config." -ForegroundColor cyan
    $bdcProxy = Get-SPServiceApplicationProxy | where {$_.GetType().FullName -eq (‘Microsoft.SharePoint.BusinessData.SharedService.’ + ‘BdcServiceApplicationProxy’)}

    $dbRule = Get-SPBusinessDataCatalogThrottleConfig -Scope Database -ThrottleType Items -ServiceApplicationProxy $bdcProxy

    Set-SPBusinessDataCatalogThrottleConfig -Identity $dbRule -Maximum $maximum -Default $defaultVal
}

C# Extension to Delete selected SharePoint List Items

It is a common requirement to delete set of SharePoint list items from a list. In most case you might want to do that quickly as well. Here is a extension method that I wrote to accomplish that.

 public static void DeleteItemsCollection(this SPList list, SPWeb web, SPListItemCollection listitemCollection)
{
StringBuilder deletebuilder = DeleteBatchCommand(list, listitemCollection);
web.ProcessBatchData(deletebuilder.ToString());
}

 

StringBuilder DeleteBatchCommand(SPList spList, SPListItemCollection listitemCollection)
{
StringBuilder deletebuilder = new StringBuilder();
deletebuilder.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string command = “<Method><SetList Scope=\”Request\”>” + spList.ID +
“</SetList><SetVar Name=\”ID\”>{0}</SetVar><SetVar Name=\”Cmd\”>Delete</SetVar></Method>”;

foreach (SPListItem item in listitemCollection)
{
deletebuilder.Append(string.Format(command, item.ID.ToString()));
}
deletebuilder.Append(“</Batch>”);
return deletebuilder;
}

The method “DeleteItemsCollection” can be called up on any list together with SPWeb and the collection list items that you want to get rid of the list. You can simply execute the method as follows.

myList.DeleteItemsCollection(web, itemsToDelete);

 

Add Quick Launch Navigation Entries together with Localization Text using Powershell

Its a time where most SharePoint developers look to do everything possible with Powershell rather than writing thousands of C# code. One common simple such requirement is to add Navigation Entries to Quick Launch. This is specially very useful when you work with a site where publishing features are not available so that you can not use the GUI to add navigation entries either.

Here is the code,

    $webURL =  “http://mySPSiteURL/&#8221;
$enTitle = “Chaminda’s Blog ”
$frTitle = “Blog de Chaminda”
$urlLink =”https://chamindasomathilaka.wordpress.com&#8221;

$webs = Get-SPWeb -Identity $webURL -ea Stop

$navigation= $webs.Navigation.QuickLaunch

$culture = New-Object System.Globalization.Cultureinfo(“fr-FR”)
$cultureFrCA = New-Object System.Globalization.Cultureinfo(“fr-CA”)

$newnode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($enTitle, $urlLink, $true)

$addedNode = $navigation.AddAsLast($newnode)

$addedNode.TitleResource.SetValueForUICulture($culture, $frTitle)
$addedNode.TitleResource.SetValueForUICulture($cultureFrCA, $frTitle)

$addedNode.Update()

$webs.Update()

That is to add a Quick Launch navigation entry directing my blog with both English and French translation (this is in case you have both English and French languages are available on your site).

Same code can be used to add Navigation entry to Top Navigation Area as well. you just need to initialize $navigation variable with correct navigation.

   $navigation= $webs.Navigation.TopNavigationBar

Changing SharePoint database from SQLExpress Edition to Default SQL Server Instance for Standalone Installation

I’m a big lover of using base machine for SharePoint development work instead of using different VMs. I love that approach,

  1. SharePoint runs much faster as it has more comfortable memory and processing power usage
  2. Visual Studio runs much faster
  3. I have access to all other tools such as MS office etc
  4. Last but no least I will not have to transfer my tools from every time I put up a new VM

I have been running SharePoint 2010 on my desktop for more than a year now, while working few different SharePoint and non-SharePoint projects.  I used this very nice article on how to setup SharePoint on Windows 7 few times now to set my dev env without noticing the SharePoint installation automatically install a SQL Server Express edition instance and make use that as the SharePoint’s database.  This was only noticed when I want to get rid of any redundant processes so that I can free up some of the processing power on the machine. For other .net related development I used to have an full SQL Server installation and addition to that my machine is running an SQL Express Edition instance for SharePoint. So I wanted to get rid of this express edition and make use of full SQL Server edition’s default instance for SharePoint as well.  So started in a hard way, uninstall my SQL express edition instance and ran the SharePoint Configuration Wizard, thinking it will notice the loss of SharePoint Configuration database and ask me to create new farm. But it didn’t go that way, instead wizard keep crashing with the message saying unable connect to SQL Server.

However finally I was able to get it to configure new farm by following steps.

  1. Got the env to disconnect from the farm by setting the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\WSS\ServerType to CLEAN_INSTALL and running the SharePoint Configuration Wizard (refer this)
  2. Then set the Server role to be Application (instead SingleServer) by setting registry entry HKLM/Software/Microsoft/Shared Tools/Web Server Extensions/12.0/WSS -> ServerRole key from ‘SingleServer’ to ‘Application’  (refer this)
  3. Then run the ShaerPoint Configration Wizard to configure new farm by providing default sql server instace (or any other SQL Server database instance) as the database

Handle with care, Json Serialization when SharePoint Object Model’s Objects in the code

My team went crazy with the generic error message that they were getting while trying to Serialize an object created of some POCO. One of them even lost good whole day trying to troubleshoot what exactly is the issue.

The serializer is one of the best in business for .Net , the Newtonsoft.Json.JsonConvert.

And the error message they were getting is

“An unhandled exception of type ‘System.StackOverflowException’ occurred in mscorlib.dll”

which hardly express any means of the cause. This error message is thrown when following piece of code is getting executed.

 JsonConvert.SerializeObject(MyEntity, Newtonsoft.Json.Formatting.Indented);

Finally I figured it out that one of the newly added Properties of MyEntity’s class causing the issue.

The newly added Property is of SPUser field.  So the entity class looked like following,

   public class MyEntity

{

public string Name { get; set; }

public string LoginName { get; set; }

public SPUser User { get; set; }

}

So the issue with the Serializer unable to serialize the instance of SPUser. The when I checked the serializer is unable to serialize SPFieldUserValue and SPFieldUserValueCollection as well.

Workaround :

Anyway fortunately I did not want this field necessarily be in the Json that I was getting.

So I decorated each of those fields with [JsonIgnoreAttribute] attribute. Doing that I had to import the Newtonsoft.Json with using directive. That made my entity class look like ,

   public class MyEntity

{

public string Name { get; set; }

public string LoginName { get; set; }

[JsonIgnoreAttribute]

public SPUser User { get; set; }

}

In conclusion, even if you use more improved serializer like Newtonsoft.json, you should be very careful about what you are trying to serialize and desterilize especially when you are dealing with SharePoint Object model’s classes.