SharePoint SPEventReceiver, RunWithElevatedPrivileges and Access Denied error

If you are coding a SharePoint event receiver and you get an access denier error even if while in a RunWithElevatedPrivileges section, the problem is that you are modifying a SPListItem taken in this way:

        SPListItem spListItem = properties.ListItem;

But the rule to follow is: in the RunWithElevatedPrivileges section, you have to use the ID properties of the properties object, to get new instances of SPSite, SPWeb and SPListItem.

So a code example is:

        SPSecurity.RunWithElevatedPrivileges(() =>
        {
            using (SPSite site = new SPSite(properties.SiteId))
            {
                using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                {
                    web.AllowUnsafeUpdates = true;
                    SPList spList = web.Lists[properties.ListId];
                    SPListItem spListItem = spList.GetItemById(properties.ListItemId);
                    this.EventFiringEnabled = false;
                    ...
                    this.EventFiringEnabled = true;
                    web.AllowUnsafeUpdates = false;
                }
            }
        }

SharePoint 2010: how define custom properties in visual web-parts

SharePoint 2010 introduces the concept of visual web-part, witch, very simply, is an ascx UserControl called from a web-part.

The fact is that you define custom properties in the web-part class, but your ascx control doesn't know the web-part, so... to define custom properties to be used in the ascx control, follow this guide:

  • define the custom property in your web-part class:
            [DefaultValue(3),
             WebBrowsable(true),
             Personalizable(PersonalizationScope.Shared),
             Category("MyCategory"),
             WebDisplayName("Max number of rows"),
             WebDescription("Max number of rows to be shown in the grid")]
            public int MaxNumberOfResults { get; set; }
  • define a web-part derived property in the web-part user-control:
            public MyWebPart WebPart;
  • assign the user-control property in the CreateChildren virtual method of the web-part:
            protected override void CreateChildControls()
            {
                Control control = Page.LoadControl(_ascxPath);
                ((WebParts_MasterLatestNewsUserControl)control).WebPart = this;
                Controls.Add(control);
            }
  • in your user-control, use the web-part properties of your web-part using the previous property:
            myGrid.PageSize = WebPart.MyCustomProperty;

You can find a more complete guide in the post How do I create custom properties in Visual Web Parts?.

SharePoint: how change the default page of a SPWeb

In SharePoint, you can change the default page of a site in two ways:

  • if the site is a PublishingWeb, cast you SPWeb to this type and set its DefaultPage property;
  • else, change the WelcomePage of its RootFolder:

SPFolder rootFolder = spWeb.RootFolder;
rootFolder.WelcomePage = "SitePages/MyHome.aspx";
rootFolder.Update();

In the documentation pages PublishingWeb.DefaultPage Property and SPFolder.WelcomePage Property there are both documentation and samples.

SharePoint 2010: build a TagCloud web-part that works with enterprise keywords tags

To build a TagCloud that works with enterprise keywords instead of social tags, the steps are the following:

  1. disable social tagging, to avoid confusion of tags;
  2. in the document libraries you want to tag, in the list settings, click the two check-boxes to enable enterprise keywords and to promote enterprise keywords tags to social tags;
  3. schedule, and run, the job called "User Profile Service Application - Social Data Maintenance Job", so that social-promoted tags are crawled and indexed by the Managed Metadata Service;
  4. eventually, at development time do a full crawl to index the data at the previous step by the search service;
  5. build a new TagCloud web-part, deriving from the existing one, and override the method GetTagCloudItemFromSocialTerm, so that it returns a correct href, pointing to the search page where you filter by the desired tag;
    important: in the constructor, remember to disable async updates, otherwise the derived method isn't called!
  6. configure the web-part to show tags from all users.

SharePoint 2010 metadata tagging

In SharePoint 2010 there are 3 types of metadata tagging:

  • social tags: these are the tags "I Like It" and "Tags & Notes" that you can find by default in the upper right corner of your pages:
    uers can enter these tags without any restrictions;
  • enterprise keywords: inserted by users in the list-item properties of lists and document libraries (after enable in the list settings):
    uers can enter these tags without any restrictions;
    while entering the data, they are suggested with previous tags inserted by themself or by other users in the past;
    tags are populated in the taxonomy (or better: folksonomy) in a flat list;
  • managed metadata:
    users must use the tags already defined by a taxonomy adminsitrator;
    the tags can be organized in a hierarchical way.

The Managed Metadata Service administrator can manage the metadata going to Central Administration, Application Management, Service Applications, Manage Service Applications, Managed Metadata Service.

You can find a good reference in the post Managing Metadata in SharePoint 2010.

SharePoint 2010 BCS limitations

In SharePoint 2010 you can use BCS to define external content types and external lists.

External lists, in comparison to internal (or standard) lists, have some limitations, essentially given because of the following reasons:

  • you can't listen to your external data source:
    • this means you cannot have event listeners, workflows and RSS feeds;
  • you cannot control the external data source:
    • this means you cannot have versioning, ratings, item permissions and attachments.

A more completed list is in the post BCS External List Limitations.

SharePoint 2010: how start the "User Profile Synchronization Service" service

If you have problems starting the "User Profile Synchronization Service" service, you can check these 3 links:

Here are some problems I personally had to solve, plus a few notes:

  • if the SharePoint installation is local, from SQL Server Configuration Manager it is needed to enable the Network Pipes protocol;
  • verify that the account executing the service is a local administrator of the machine;
  • verify that the account executing the service can logon to the machine;
  • make a logon to the machine with the account executing the service, then reboot the machine.

Finally: the services to start are:

  • User Profile Service
  • User Profile Synchronization Service

For the second one expect a rather long time to start.

SharePoint 2010 and PowerShell guide for wsp deployment

Here is a list of common PowerShell commands to be used with SharePoint 2010 when managing wsp files:

  • Add-SPSolution c:\folder\MyProject.wsp
  • Install-SPSolution –Identity MyProject.wsp –WebApplication http://mywebapp -GACDeployment -Force
  • Install-SPSolution –Identity MyProject.wsp –AllWebApplications -GACDeployment -Force
  • Update-SPSolution –Identity MyProject.wsp –LiteralPath c:\folder\MyProject.wsp –GACDeployment -Force
  • Uninstall-SPSolution –Identity MyProject.wsp –WebApplication http://mywebapp
  • Uninstall-SPSolution –Identity MyProject.wsp –AllWebApplications
  • Remove-SPSolution –Identity MyProject.wsp

Adding and Deploying Solutions with PowerShell in SharePoint 2010 is a useful reference page for the PowerShell commands for wsp management.

And Force Reinstall of SharePoint 2010 Solution through Visual Studio 2010 or PowerShell is a page describing the problem on forcing the reinstallation of the features when deploying the wsp, even if you get an error saying the feature is already installed. This is the reason why I add the -Force parameter to the the Install-SPSolution commands.