Wednesday 21 December 2011

Unable to cast COM object of type 'Microsoft.SharePoint.Library.SPRequestInternalClass' to interface type 'Microsoft.SharePoint.Library.ISPRequest'.

I was working with PowerShell and Sharepoint 2010 and for some reason I have got this error:

Unable to cast COM object of type 'Microsoft.SharePoint.Library.SPRequestInternalClass' to interface type 'Microsoft.SharePoint.Library.ISPRequest'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{BDEADEBE-C265-11D0-BCED-00A0C90AB50F}' failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE)).

This is the only stuff I was doing was adding a new library to my existing site. This is the code:

 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
 $spWeb = Get-SPWeb -Identity "http://sp_foundation_g/sites/Documents"
 $spWeb.Lists.Add("AllDocuments","My Doc Library",[Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary)
If you use Powershell you normally are administrator, but just in case I decided to check RunWithElevatedPrivileges in order to avoid any problem with the security. Everything was fixed straight away, and I didn’t get the error anymore, even if I tried to run the code without RunWithElevatedPrivileges. This is the code I ran to fix it:
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(  
 {      
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
     $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
     $spWeb = Get-SPWeb -Identity "http://sp_foundation_g/sites/Documents"
     $spWeb.Lists.Add("AllMydocuments","My Doc Library",[Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary)
 }
)  
Conclusion: I have done plenty of research but I can’t find the answer of why this is happening, but the good news, it can be fixed!.

Tuesday 20 December 2011

Listing all the Content Types and Fields with PowerShell in Sharepoint 2010

For many reasons at some point you will need to know how many content types you have and all the fields in a particular content type. I wrote a nice script few weeks ago you can run very easily.

Use PowerGui to run it or just copy/paste this code in a file, save it and run it with the native PowerShell.

	write-host ("...Listing Content Types")
	
	$url=  "http://sp_foundation_g/sites/Documents"
	$site = get-spsite $url 
	
	$web = $site.OpenWeb() 
	
	foreach ($contenttype in $web.ContentTypes)
	{	write-host("##################")
		write-host("Content Type Name:" + $contenttype.Name)
		write-host("Fields:")
		foreach ($field in $contenttype.Fields)
		{
			write-host($field.get_InternalName())
		}
	}
	

Enjoy!

Nice tools to work with Powershell under the Sharepoint 2010 scope.

I am sure that many of you already know these tools, but sometimes it takes me a while to find the stuff on Internet, so I was thinking to pack my favourite ones in one post, this is the result.

PowerGui:
A beautiful GUI to program with PowerShell. It allows you to do everything, it doesn’t eve allow you to modify the user Interface.
Download the software from here, it is FREE! and run this command: Add-PSSnapIn Microsoft.SharePoint.Powershell in PowerGui in order to get the Sharepoint commands.

Windows PowerShell Command Builder:
Developed by Microsoft in Silverlight, it will give you the best overview of command creation for Sharepoint on the net, it is unbeatable, to download it, go here. Check the screenshot!
image