Wednesday, August 31, 2011

Error updating User Profile property on commit

Error: Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.


I saw many people has came across this is error when updating user profile property values, but I couldnt find a solid solution to this issue. So thought of posting a article on this one. The error may be misleading. Many think web.AllowUnsafeUpdates=true will solve the issue. But it wouldn't because this update will not have any impact on the User Profile Service application. 

Try the below code, it should solve the issue.





The difference here is SPContext.Current.Web.AllowUnsafeUpdates=true will update the current user profile to allow updates. Also make sure that there is no context given when Instantiating UserProfileManager object as it gets populated automatically when run on a personal site context. 

Tuesday, August 30, 2011

Hide Any Site template in Create Site section- SharePoint 2010

We may need to block creation of sites based on certain templates. For example my organisation does not like the employees to create blog site of their own in their personal mysite. 

There is a quick and dirty way to modify the webtemp.xml file to hide the template we dont want to show up. Its never a good approach to modify the default SharePoint config files. Also webtemp.xml file modification will affect farm level settings.

In SharePoint 2010 we can hide the templates of desired nature from the Create site page so no one can create site based on the particular template. This can by achieved by creating a new feature with a event handler code.

1. Create a new Empty SharePoint project.
2. Add a new feature element.Scope it to Web
3. Add a new event receiver to that feature element.
4. Paste  the below code(this code will hide the blog template at the site level)

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
SPWebTemplateCollection existingWebTemps = web.GetAvailableWebTemplates(1033); ;
Collection<SPWebTemplate> newWebTemps = new Collection<SPWebTemplate>();
for (int i = 0; i < existingWebTemps.Count; i++)
{
if (!existingWebTemps[i].Title.ToLower().Contains("blog"))
{
newWebTemps.Add(existingWebTemps[i]);
}
}
web.SetAvailableWebTemplates(newWebTemps, 1033);
web.Update();
}

5.Deploy the solution and activate the feature. You will no longer see the blog template visible to the user in the Create site page.

6. To revert back and to show all the templates use the below code at Feature deactivating event.

 web.AllowAllWebTemplates() ;

Saturday, August 6, 2011

Get User profile using Console App- Object reference error at Server context

Today I was trying to retrieve user profiles properties using a console application. Its a very simple application just pulls some metadata from the userprofilesmanager. Previously I have get and set profile properties in my SharePoint application many times. Thought it would be a piece of cake for me. Not though.

This is the code I have written in the program.cs. But to my surprise I was getting object reference error. I was thinking that url might be the wrong thing. But its not.


            SPSite site = new SPSite("http://suresh-pc:100/default.aspx");
            ServerContext context = ServerContext.GetContext(site);
            UserProfileManager upm = new UserProfileManager(context);
            UserProfile profile = upm.GetUserProfile("moss/suresh");

This error is a misleading one. You dont have permission to pull data from User Profile Service application basically.

Go to Central Admin- Manage Service applications- Click User Profile Service Application(click the row not the link).- In the ribbon select Manage Permmissions


Provide full permission for the user trying to access the User profile manager.

Build the application and run again.