Last post….

Hi,

Unfortunately, I’ll no longer post on this blog. I’ve refocused my career on a different direction and I’m no longer working directly with SharePoint. I’ll leave the blog up and running for now since I still can help some people with some SharePoint issues.

Yours,

Andy.

Force SharePoint 2010 Search Scope Update

For those wondering how to force a scope update and not having to wait 14 minutes for each update, this can help.

  • Go to Central Administration site
  • Go to your Search Service Application (Manage Services -> Search SA)
  • On the left hand side, click on Search Administration link under Administration
  • Unders System Status, look for an entry saying Scopes needing update and click on the Start Update Now link (see below)

Pretty straightforward !

Hope that helps !

Andy Nogueira.

Access Denied Messages when retrieving SharePoint User Profiles Count even running in Elevated code

This is another tricky one. And I think I had this problem long time ago which I had this workaround but couldn’t remember about it until I’ve looked into an old code snippet.

Here’s the scenario.

You need to retrieve the total number of user profiles in SharePoint. The API has a property for that in the UserProfileManager class. You could just write some code like this:


UserProfileManager profileManager = new UserProfileManager(serviceContext);
long ProfileCount = profileManager.Count;

But the problem is that in order to retrieve this you need to have admin privileges on the User Profile Service. So the logical way of doing this is to run the code with Elevated privileges and ensure the identity used by the application pool of your site has admin privileges on the User Profile service. So I’ve tried that using the code below:


SPSite siteColl = SPContext.Current.Site;

try{
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
    {
      SPServiceContext serviceContext = SPServiceContext.GetContext(ElevatedsiteColl);
    UserProfileManager profileManager = new UserProfileManager(serviceContext);
    ProfileCount = profileManager.Count;
    }
  });
}
catch (Exception ex)
{
  LogException(ex);
}

Testing the code running under an Admin account it was fine (because it was the same account used by the site’s application pool). But when I tested the code using a regular user this code should still run fine (because I was running it elevated). Unfortunately that’s not what happened. For some weird reason seems that the SharePoint API was trying to impersonate the current user thus throwing an exception and showing an Access Denied error which said  Access Denied: Only an administrator may retrieve a count of all users.

So after sleepless nights (just kidding it was just some banging my head on the wall), I finally remembered to take a look at an old code snippet that I had when facing the same issue in the past, and I’ve decided to write this post (blog posts are a great way to use as a diary).

The Workaround

Basically my workaround is to set the current HttpContext to null inside the code, but keeping the existing context in a temp variable so I can revert it back once I run the snippet. You have to revert it back to the original context otherwise you are going to get a lot of errors. The code looks like this: 

SPSite siteColl = SPContext.Current.Site;
HttpContext tempCtx = HttpContext.Current;

try
{
SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
{
SPServiceContext serviceContext = SPServiceContext.GetContext(ElevatedsiteColl);
HttpContext.Current = null;
UserProfileManager profileManager = new UserProfileManager(serviceContext);
ProfileCount = profileManager.Count;} });
}
catch (Exception ex){ LogException(ex);}
finally{ HttpContext.Current = tempCtx;}

Using the code above I could retrieve the count even using a regular user (no admin access) .

 I hope this saves some time on your side and avoid some head banging on the wall

Have a nice one !

Andy Nogueira.

SPWeb.CurrentUser returns SHAREPOINT\System not the real user name.

If you try to get the current user login name or name in SharePoint and if it shows SHAREPOINT\System account, that’s because the current logged user is an admin account and is probably the one used in the application pool of the site. Also, you are probably getting the current user from an elevated SPWeb inside a RunWithElevatedPrivileges code. You can use the snippet below to get the real user:


SPWeb site = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
  {
    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
    {
      //Don't use the ElevatedSite.CurrentUser
      string currUser = site.CurrentUser;
    }
  }
});

This will show the real login name instead of SHAREPOINT\System

Bear in mind that the code above is just a snippet and to show you the idea. Please make sure you use the SharePoint best practices coding for disposing objects when using the snippet above.

Hope that helps !

Changing namespace in Visual Studio 2010 might break a SharePoint 2010 webpart project

Just want to create a post about this since it’s the second time this happened to me and I wished this second time I had created this post when I experienced this error for the first time so I could check my own blog and figure out what was happening. Blogs can be used as journals too for our own help :-)

I was creating a SharePoint 2010 project in Visual Studio 2010. I’ve selected the Empty Project and then Add New Item and selected the Webpart item (not the Visual Webpart). After doing some coding, for some reason I’ve decided to change the original namespace that Visual Studio had created for me on the webpart class (.cs file). Then I did some more coding and time to test it. I right click on the project name and select Deploy (I love this new feature in VS2010). Everything goes smooth, package is deployed and everything seems fine. So I go to my SharePoint site, Edit the page, Insert a Webpart, select my new custom webpart and when I hit the Add button, for my surprise I got the error below.

And this is the second time around that this happened to me. The first time was a long time ago so I couldn’t remember why. Then it’s time to do some troubleshooting. I’ve verified that the .DLL file was in the GAC, I’ve checked the web.config file to make sure a <SafeControl> entry was created. Everything looks good. Then I check the webpart definintion file in the WebParts gallery and closely looking at the XML I’ve noticed that the namespace was not matching with my webpart class. The original namespace was still showing. So I went back to my Visual Studio project and I’ve opened the .webpart file (see below) that was created for me.

And for my surprise the original namespace was still showing up there. So if you change your namespace after you create a project in VS2010 make sure that in the .webpart file (see below) under the <Metadata> <type> elements, the name attribute has the correct namespace.


<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="SP2010.WebParts.MembesListingWebPart.MembersListingWebPart, $SharePoint.Project.AssemblyFullName$" />
<importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Members Listing WebPart</property>
<property name="Description" type="string">This WebPart lists the members of a site</property>
</properties>
</data>
</webPart>
</webParts>

Another change you have to do and this one is a tricky one is on the Visual Studio 2010 SharePointProjectItem.spdata file. This is a file I believe Visual Studio uses internally to package and deploy the solution. But the catch is that it doesn’t show by default, so you have to click on the “Show All Files” button in the Solution Explorer and this file will show inside your WebPart folder (see below). Edit the file and change the Namespace attributes according to your new namespace.

Also, don’t forget in that if you change the namespace, in your project properties, make sure it’s reflected in the Default Namespace field in the Application tab of your Project properties (right-click your project name and select properties).

Hope this will help some people save some troubleshooting time !

Manage Access Requests not showing on a SharePoint 2010 site

The other day I was just trying to get to the Manage Access request option on SharePoint 2010 and for my surprise I couldn’t find the link to get to the page.

So in order to figure it out, I went to a MOSS 2007 site, and I knew I could get to it going to Site settings -> Advanced permissions -> Settings -> Access requests, this showed me that this page name and location was /_layouts/setrqacc.aspx. I thought that Microsoft probably haven’t changed this on SharePoint 2010. So I went back to my SP2010 server and appended this location to my site’s URL. For my surprise I got an error message that gave me an insight on what was going on. The message said something about emails not being configured.

So I went to the SharePoint 2010 Central Admin site, and under System Settings -> Configure outgoing e-mail settings, once I configured the smtp server and outgoing email.

Then I tried to access the page again and it worked. I’ve also realized that one option in SharePoint 2010 that I was not seeing before started to show up, and that was a link on the Ribbon going to Site Settings -> Site Permissions. The link that said Manage Access Requests (see below) started to show up.

SilverPart 2.1 release, a SharePoint webpart to expose Silverlight applications

I’ve just released a new update of the SilverPart on CodePlex. For those who don’t know this is an open source webpart I’ve created some time ago and it allows you to run Silverlight applications in WSS 3 and MOSS 2007.

SharePoint 2010 comes with a webpart like this OOTB, but in the old SharePoint (2007) this was not available. The nice thing about it was that I’ve created it in a way that you didn’t have to install additional software such as the ASP.NET Silverlight control.

This interim release 2.1 fixes some reported issues with using it with Firefox and anonymous access.

You can download it from http://silverpart.codeplex.com

Cheers

Andy Nogueira

P.S – What I really liked about CodePlex is that now you can insert Ads on their page and I’ve opted to donate the Ad revenue, so I’m not only giving back to the community but I’m also helping a good cause.