Activate Claim Based Authentication afterwards with PowerShell

You can active Claim Based Authentication for a SharePoint Web App afterwards if you missed that at creation time.

Use this PowerShell script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadFile($Env:CommonProgramFiles+"Microsoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.dll") | out-null

$webapp = Get-SPWebApplication "http://<sharepoint server>/"
write-host "Current: " $webapp.UseClaimsAuthentication
$webapp.UseClaimsAuthentication = $true
$webapp.Update()

$webapp.ProvisionGlobally()

write-host "    New: " $webapp.UseClaimsAuthentication

Enable CollectSPRequestAllocationCallStacks with PowerShell

I got this message in the ULS log:

An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  This object will now be disposed.  Allocation Id: {646667A7-73BC-4DDD-B0FB-6EDFC315CCE7}  To determine where this object was allocated, set Microsoft.SharePoint.Administration.SPWebService.ContentService.CollectSPRequestAllocationCallStacks = true. 

The following PowerShell script will do that for me – and you 🙂

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadFile($Env:CommonProgramFiles+"Microsoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.dll") | out-null

# Get Content Service of the farm
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

# Display and change the setting of property "CollectSPRequestAllocationCallStacks"
write-host "Current: " $contentService.CollectSPRequestAllocationCallStacks 
$contentService.CollectSPRequestAllocationCallStacks = $true
$contentService.Update()

write-host "    New: " $contentService.CollectSPRequestAllocationCallStacks 

Add web.config Modification with PowerShell (SPWebConfigModification)

Here is a script I used to add some web.config modifications with PowerShell. In this case I want to add a custom authentication provider. – The following script I used for setup purpose.

# Load SharePoint PowerShell PSSnapIn and the main SharePoint .net library
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$sharePointDLL = $ENV:CommonProgramFiles+("Microsoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.dll")
[System.Reflection.Assembly]::LoadFile($sharePointDLL) | out-null

# Show Farm BuildVersion to ensure the SharePoint .net library is loaded
$localFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$localFarm.BuildVersion

# store some settings and objects in variables
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)
$farmServices = @($webapp.Farm.Services | where-object { $_.TypeName -eq "Microsoft SharePoint Foundation Web Application" } )[0]
$assembly = "MyAuthenticationProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx"

# Remove old web.config modifications of MyAuthenticationProvider
$oldMods = @();
$webapp.WebConfigModifications | where-object { $_.Owner -eq "MyAuthenticationProvider" } | foreach-object { $oldMods = $oldMods + $_}

$oldMods | foreach-object{ $webapp.WebConfigModifications.Remove($_) }

# update the Web Application and apply all existing web.config modifications - this executes the "remove" actions from above
$webapp.Update()
$farmServices.ApplyWebConfigModifications()

# New web.config modifications for MyAuthenticationProvider
$myModification1 = new-object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$myModification1.Path = "configuration/system.web/membership/providers"
$myModification1.Name = "add[@name='MyAuthenticationProvider'][@type='MyAuthenticationProvider.MyMembershipProvider, " + $assembly + "']"
$myModification1.Sequence = 0
$myModification1.Owner = "MyAuthenticationProvider"
$myModification1.Type = 0           #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$myModification1.Value = "<add name='MyAuthenticationProvider' type='MyAuthenticationProvider.MyMembershipProvider, " + $assembly + "' />"
$webapp.WebConfigModifications.Add($myModification1)

$myModification2 = new-object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$myModification2.Path = "configuration/system.web/roleManager/providers"
$myModification2.Name = "add[@name='MyAuthenticationProvider'][@type='MyAuthenticationProvider.MyRoleProvider, " + $assembly + "']"
$myModification2.Sequence = 0
$myModification2.Owner = "MyAuthenticationProvider"
$myModification2.Type = 0           #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$myModification2.Value = "<add name='MyAuthenticationProvider' type'MyAuthenticationProvider.MyRoleProvider, " + $assembly + "' />"
$webapp.WebConfigModifications.Add($myModification2)

# Update the Web Application and apply all exisiting web.config modifications including the new from above

$webapp.Update()
$farmServices.ApplyWebConfigModifications()

Please notice my disclaimer in the right sidebar!

“TaxonomyPicker” failed to load – Error in Event Viewer

After updating the SharePoint Foundation 2010 Server of our company to SharePoint Server 2010 I got this error in the Event Viewer:

Load control template file /_controltemplates/TaxonomyPicker.ascx failed: 
Could not load type 'Microsoft.SharePoint.Portal.WebControls.TaxonomyPicker' 
from assembly 'Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.

I could solve this error by re-enabling the corresponding feature using this PowerShell script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadFile($Env:CommonProgramFiles+"Microsoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.dll") | out-null
Get-SPWebApplication -IncludeCentralAdministration | get-spsite | foreach-object { enable-spfeature "73ef14b1-13a9-416b-a9b5-ececa2b0604c" -force -url $_.Url -Verbose }

Note: The first to lines of the script enable are “standard lines of code” for me. They enable SharePoint PowerShell support if you use the Script without “SharePoint 2010 Management Shell”.

How To Activate SharePoint Ribbon Tab by JavaScript Code

In the last weeks I’ve extended the sample project that I’ve created for “Ribbon Customization”. You find the project source code on Codeplex:

The related articles on my blog are:


There are lots of excellent articles out there about SharePoint 2010 Ribbon customization.

Some articles that helped me:

Especially the first blog post in the above list leaves a question open: "Using client-side code to show ribbon items".

I had the same problem: How to activate a custom SharePoint Ribbon tab by JavaScript code?

After some reading in SharePoint standard JavaScript files I found the solution. I used some jQuery too:

1: $(document).ready(function  () {

 

2:     ExecuteOrDelayUntilScriptLoaded(salesappActiveRibbonTab, "sp.ribbon.js" );

 

3:

 

4:     function  salesappActiveRibbonTab() {

 

5:         try  {

 

6:             _ribbonStartInit("MyApp.SharePoint.Ribbon.CustomTab" , false , null );

 

7:         } catch  (e) {

 

8:         };

 

9:     };

 

10: });

This allows me to activate my custom Ribbon tab with ID "MyApp.SharePoint.Ribbon.CustomTab" by JavaScript code. It works pretty fine!

Update 1

Meanwhile I created an test project for adding a custom ribbon tab while runtime. There I have had the problem, that the ribbon was created correctly BUT was not activated thru the code above. After some debugging of MS Ribbon JavaScript I created the following code:

var ribbontestintervall = null;

 

function RibbonTestActiveRibbonTab2() {     try {         window.clearInterval(ribbontestintervall);

 

        if (typeof (_ribbonStartInit) == "function")             _ribbonStartInit('RibbonTest.SharePoint.Ribbon.CustomTab', false, null);

 

        if (true && typeof (_ribbonReadyForInit) == 'function' && !_ribbonReadyForInit()) {             ribbontestintervall = window.setInterval("RibbonTestActiveRibbonTab2()", 1000);         }     } catch (e2) {     };
};

So the Browser retries the activation of the tab as long as it works.

Update 2 (2011/01/14)

See my new post and sample project on CodePlex :

Sample Visual Studio 2010 project for creating a custom SharePoint 2010 Ribbon tab on runtime (!)

Update 3 (2011/03/03)

Next part posted today. See the part list at the top of this blog post.

SharePoint 2010 Application Page Code Behind Throws Exception “Trying to use an SPWeb object that has been closed or disposed and is no longer valid.”

This was the situation:

I have a SharePoint Foundation 2010 project in VS2010. There I deploy some SharePoint lists with custom views an custom form pages.

In one case I wanted to override the RaisePostBackEvent method of the customized form page. In the method I wanted to do some actions before the Save button code is executed. (Its the standard Save button of a SharePoint list form: <SharePoint:SaveButton...>.

The overridden RaisePostBackEvent locked like this:

1: protected  override  void  RaisePostBackEvent(System.Web.UI.IPostBackEventHandler  sourceControl, string  eventArgument)
2:         {
3:             if  (sourceControl is  System.Web.UI.WebControls.Button )
4:             {
5:                 System.Web.UI.WebControls.Button  button = ((System.Web.UI.WebControls.Button )sourceControl);
6:                 if  (button.ClientID.StartsWith(savebutton1.ClientID) || button.ClientID.StartsWith(savebutton2.ClientID))
7:                 {
8:                     using  (SPWeb  web = SPContext .Current.Web)
9:                     {
10:                         //some code 
11:                     }
12:                 }
13:             };
14:
15:             base .RaisePostBackEvent(sourceControl, eventArgument);
16:         }
17:
18:

When executing the custom list form page there was no errors when I filled all field input boxes with data and press the Save button. But as I tried the “required field” functionality – leaving some fields blank – the SharePoint system throws the following error message in the SharePoint log:

06/15/2010 18:11:19.94 	w3wp.exe (0x1384)                       	0x0E5C	SharePoint Foundation         	Runtime                       	tkau	Unexpected	Microsoft.SharePoint.SPException: Trying to use an SPWeb object that has been closed or disposed and is no longer valid.    at Microsoft.SharePoint.WebPartPages.SPWebPartManager.ThrowIfManagerIsInvalid()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.get_LimitedWebPartManager()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadConnectionsState()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadWebParts()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.OnPageInitComplete(Object sender, EventArgs e)     at System.EventHandler.Invoke(Object sender, EventArgs e)     at System.Web.UI.Page.OnInitComplete(EventArgs e)     at Sal...	b196095c-b664-4f3c-b764-f9b3ef8ac5db
06/15/2010 18:11:19.94*	w3wp.exe (0x1384)                       	0x0E5C	SharePoint Foundation         	Runtime                       	tkau	Unexpected	...esAppLists.Layouts.SalesApp.AddDecisionMakerForm.OnInitComplete(EventArgs e)     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)	b196095c-b664-4f3c-b764-f9b3ef8ac5db
0

After some debugging and thinking about it I realized that it only happens when the I press the Save button and this action will not close the dialog frame of the list form page. This means: The postback action does more than “store data and leave the dialog frame”.

So I figured out that there is a overridden RaisePostBackEvent method on the page.

I removed the method for testing purpose… and the problem was away!

It seems to me that it is not possible to use using (SPWeb web = SPContext.Current.Web) in such a method.

That’s me!

Hi,

I’m Ingo. Sometimes I do some SharePoint. I’d like to share some knowledge with you. I hope, you’ll respond to it. May be you have questions. I have much of it. May be you have answers for me. I’d be glad to hear it!

See you!
Ingo