Friday 28 February 2014

Operation is not valid due to the current state of the object.

Error :Exception Source : Microsoft.SharePoint
Stack Trace : at Microsoft.SharePoint.SPUserToken.GetClaimsUserLoginName()
   at Microsoft.SharePoint.SPSite.CopyUserToken(SPUserToken userToken)
   at Microsoft.SharePoint.SPSite.SPSiteConstructor(SPFarm farm, Guid applicationId, Guid contentDatabaseId, Guid siteId, Guid siteSubscriptionId, SPUrlZone zone, Uri requestUri, String serverRelativeUrl, Boolean hostHeaderIsSiteName, SPUserToken userToken, Boolean appWebRequest, String appHostHeaderRedirectDomain, String appSiteDomainPrefix, String subscriptionName, String appSiteDomainId, Uri primaryUri)
   at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, Boolean swapSchemeForPathBasedSites, SPUserToken userToken)
   at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
   at Microsoft.SharePoint.SPSite..ctor(String requestUrl, SPUserToken userToken)
Exception Message: Operation is not valid due to the current state of the object.
SharePoint Log: [Forced due to logging gap, cached @ Original Level: Verbose] SQL connection time: 0.2037 for Data Source=;Integrated Security=True;Enlist=False;Pooling=True;Min Pool Size=0;Max Pool Size=100;Connect Timeout=15;Application Name=SharePoint[w3wp][2]   0x1358  SharePoint Foundation                 Authentication Authorization    ajmmt   High       [Forced due to logging gap, Original Level: VerboseEx] SPRequestParameters: AppPrincipal={0}, UserName={1}, UserKye={2}, RoleCount={3}, Roles={4} w3wp.exe SharePoint Foundation        ak8dj     High       UserAgent not available, file operations may not be optimized.    at Microsoft.SharePoint.SPFileStreamManager.CreateCobaltStreamContainer(SPFileStreamStore spfs, ILockBytes ilb, Boolean copyOnFirstWrite, Boolean disposeIlb)     at Microsoft.SharePoint.SPFileStreamManager.SetInputLockBytes(SPFileInfo& fileInfo, SqlSession session, PrefetchResult prefetchResult)     at Microsoft.SharePoint.CoordinatedStreamBuffer.SPCoordinatedStreamBufferFactory.CreateFromDocumentRowset(Guid databaseId, SqlSession session, SPFileStreamManager spfstm, Object[] metadataRow, SPRowset contentRowset, SPDocumentBindRequest& dbreq, SPDocumentBindResults& dbres)     at Microsoft.SharePoint.SPSqlClient.GetDocumentContentRow(Int32 rowOrd, Object ospFileStmMgr, SPDocumentBindRequest& dbreq, SPDocumentBindResults& dbres...  

One of the major issue in designer workflow 2013 was that we were using User Token to kick off the workflow because it can not be triggered directly using System account due to security reasons. It took around 1 month to resolve it. The user which we were using to Kickoff the workflow was getting expired if the Site was not getting used for more than 24 hours. The reason for 24 hour is that the SharePoint stores the User Token in cache from the logging time. Hence if the User does not login manually or via code its gives a error of Operation is not valid due to the current state of the object. Once the User login in the System manually the problem gets rectified. If you go to  Site Settings-> Site Permissions-> Check Permission it gives a error same error of Operation is not valid due to the current state of the object.

Solution: To kickoff the workflow Use Following piece of code:SPContext.Current.Site.SystemAccount.UserToken

using (SPSite oSPSite = new SPSite(SPContext.Current.Web.Url, SPContext.Current.Site.SystemAccount.UserToken))
            {
                using (SPWeb oSPWeb = oSPSite.OpenWeb())  //Subsite URL
                {
                    oSPWeb.AllowUnsafeUpdates = true;
                               foreach (var workflowSubscription in subscriptions)
                            {
                                if (workflowSubscription.Name == "Workflow Name")
                                {
                                    //initiation parameters
                                    var inputParameters = new Dictionary<string, object>();
                                    inputParameters.Add("Comments", "TEST");
                                    inputParameters.Add("ItemId", item.ID);
                                    inputParameters.Add("WorkflowStart", "StartWorkflow");
                                    workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, item.ID, inputParameters);
                                    break;
                                }
                            }
  oSPWeb.AllowUnsafeUpdates = false;
This piece of code will use app pool account token which never expires and hence the problem solved. Regarding the Security if I am using User token of my app pool account it gives me access to content DB and Workflow manager.
 

1 comment:

  1. Dear Please help with a related issue. I read your comment on this page... http://stackoverflow.com/questions/21037734/how-to-resolve-error-while-trying-to-publish-2013-workflow-via-designer-probabl ...and I am facing the same problem. On that page you you wrote... "make sure you have activated the App management Service in your site features" ...but I cannot find "app management service" in my Site Features and in my Site Collection Features . Can you explain? Thanks. -- Mark Kamoski

    ReplyDelete