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:
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:[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)
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!.[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)})