Question

I am developing asp.net mvc application for SharePoint Foundation 2010. asp.net application is hosted on one machine and SharePoint Foundation 2010 is hosted on another machine. I have deployed one custom wcf service inside ISAPI folder of SharePoint environment. This custom wcf service creates content databases programatically. asp.net application consumes this custom wcf service. I am using this approach because my scenario is similar to the link - http://blog.sharedove.com/adisjugo/index.php/2012/07/31/creating-site-collections-in-specific-content-database/. Implemeting the above scenario I was getting error - http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopmentprevious/thread/f10d009b-13c4-4942-86b4-2ab8c4f5994d. I have solved this error with http://beyondweblogs.com/sharepoint-2010-sppersistedobject-access-denied/. Now I am getting error - System.InvalidOperationException: Operation is not valid due to the current state of the object.

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
   at Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest()
   at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
   at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Add(T newObj, Boolean ensure)
   at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Ensure(T newObj)
   at Microsoft.SharePoint.Administration.SPContentDatabaseCollection.Add(Guid newDatabaseId, String strDatabaseServer, String strDatabaseName, String strDatabaseUsername, String strDatabasePassword, Int32 warningSiteCount, Int32 maximumSiteCount, Int32 status, Boolean provision, Guid lockId, Int32 addFlags)
   at Microsoft.SharePoint.Administration.SPContentDatabaseCollection.Add(String strDatabaseServer, String strDatabaseName, String strDatabaseUsername, String strDatabasePassword, Int32 warningSiteCount, Int32 maximumSiteCount, Int32 status)
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.<createContentDb>b__7()
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.createContentDb()
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.<>c__DisplayClass3.<GetName>b__1()
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.GetName()

This error is described in above link bolg.sharedove.com. I think I need to set the following code as described in bolg.sharedove.com

var storeContext = HttpContext.Current; 
HttpContext.Current = null; 
// do your code 
HttpContext.Current = storeContext;

I thnik I can not set the above code in wcf service. I tried to set it in asp.net application but same error has occured. Can anuone please tell where should I set this code ? If anyone is having different solution then please share.

Was it helpful?

Solution

I simply added reference to System.Web.dll in wcf service application. Now I can use HttpContext in wcf. I have set the following code in wcf

var storeContext = HttpContext.Current; 
HttpContext.Current = null; 
// do your code 
HttpContext.Current = storeContext;

Now all the code in wcf is working fine. I am now able to create content database and site collection programatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top