Elevated Privilages in Event Receivers

I am trying to reset item permissions on ItemAdded and ItemUpdated events, wrote below code to do this job for any user (even for users who don’t have manage permissions on the list item):

SPListItem oItem = properties.ListItem;
Guid siteId = properties.Web.Site.ID;
Guid webId = properties.Web.ID;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteId))
    {
        using (SPWeb oWeb = site.OpenWeb(webId))
        {
            Utility.removeExistingPermissions(oItem);
        }
    }
});

Though removeExistingPermissions() function is calling from elevated block it is still using current user credentials and throwing access denied error. Mistake I was doing is creating list item from properties.ListItem.

We should create list item from elevated web and not from properties, correct code is below:

Guid siteId = properties.Web.Site.ID;
Guid webId = properties.Web.ID;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteId))
    {
        using (SPWeb oWeb = site.OpenWeb(webId))
        {
            SPListItem oItem = oWeb.Lists[properties.ListId].GetItemById(properties.ListItemId);
            Utility.removeExistingPermissions(oItem);
        }
    }
});

Error Message: Cannot add the specified assembly to the global assembly cache

I got below error message when try to deploy solution from Visual Studio:

Error occurred in deployment step ‘Add Solution’: Error: Cannot add the specified assembly to the global assembly cache: MyLibrary.dll.

Manually uninstalling dll from GAC and Visual Studio restart fixed my issue. Also, restarted owstimer, not sure if that makes any difference.

Enable Term Store Managerment on Blank Site

“Term store management” option is not available if you create your site collection with “Blank Site” template. You need to enable Managed Metadata Service feature using following PowerShell commands:

Enable-SPFeature -id “73EF14B1-13A9-416b-A9B5-ECECA2B0604C” -Url http://myVMSP2010:14888/
Enable-SPFeature -id “7201D6A4-A5D3-49A1-8C19-19C4BAC6E668” -Url http://myVMSP2010:14888/

Code Snippet: Get distinct values from list column using IEnumerable

This code snippet shows how to get distinct values from a list column using IEnumerable interface.
For example, I have a column called “CarMake” and it contains user defined values and need to get distinct values of all car makes.


// Get Links where Collection is EMPTY (get Distinct)
SPQuery oQuery = new SPQuery();
oQuery.Query = "";
oQuery.ViewFields = "";
SPListItemCollection itemColl = oList.GetItems(oQuery);

// Get Distinct values from CarMake and Sort
IEnumerable distinctList = itemColl.Cast()
    .Select(item => item["CarMake"].ToString())
    .OrderBy(item => item["CarMake"])
    .Distinct();

// Loop through each value
using (IEnumerator enumerator = distinctList.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        string tempStr = enumerator.Current.ToString();
    }
}

SharePoint – Filter Lookup Field Values by Current User

We have a requirement to filter Look-up drop-down values based on the current logged in user.

Incident List:

Title Single Line of Text
Description Multiple Lines of Text
Site Lookup to “Sites” list (single select)

By default, “Site” lookup field will display all items from “sites” list. But, in our case drop-down should show only sites assigned to current user.

“Sites” List:

SiteName UserName
Site A User1
Site B User2; User3
Site C User3

Leveraged SP services and implemented client side code to achieve this functionality.

In this case, user2 will only see “Site B”.

Continue reading

SharePoint 2010 Technical Articles for Developers

Business Connectivity Services Technical Articles for SharePoint 2010
Claims and Security Technical Articles for SharePoint 2010
Enterprise Search Technical Articles for SharePoint 2010
SharePoint 2010 Development Basics Technical Articles
Workflow Technical Articles for SharePoint 2010
Adding a Custom Action to a SharePoint 2010 Display Form for List Items
Application Lifecycle Management in SharePoint 2010
A Brief Introduction to Enterprise Metadata Management for Microsoft SharePoint Server 2010 Developers
Creating a Custom Web Part Editor in SharePoint 2010
Creating Custom SharePoint Server 2010 Field Types and Field Controls with Visual Studio 2010 and Silverlight 3
Customizing and Extending the SharePoint 2010 Server Ribbon
Customizing SharePoint Online with SharePoint Designer 2010
Debugging and Logging Capabilities in SharePoint 2010
Deploying Branding Solutions for SharePoint 2010 Sites Using Sandboxed Solutions
Developing for SharePoint 2010 Excel Services Using Web Services or the Excel Services REST API
Developing with SharePoint 2010 Word Automation Services
Integrating Custom Silverlight 4 Applications with SharePoint Server 2010
Migrating Managed Metadata in SharePoint Server 2010
Opening a SharePoint 2010 Modal Dialog Box from an InfoPath 2010 Form
Overview of the Fluid Application Model in SharePoint Foundation 2010
Processing Documents in Bulk Using SharePoint 2010 and Open XML 2.0
Real World Branding with SharePoint 2010 Publishing Sites
Redeploying Customizations and Solutions in SharePoint Foundation 2010 and SharePoint Server 2010
Routing a Record to a SharePoint 2010 Document Library Subfolder
SharePoint Online: An Overview for Developers
Team-Based Development in SharePoint 2010
Understanding Visual Upgrade in SharePoint 2010
Using Event Receivers in SharePoint Foundation 2010 (Part 1 of 2)
Using Event Receivers in SharePoint Foundation 2010 (Part 2 of 2)
Using Network Monitor with the Microsoft Office and SharePoint Products Parsers
Using the SharePoint Foundation 2010 Managed Client Object Model
Using the SharePoint Foundation 2010 M