Custom Module Error Trying to Publish
Hi,
Alot of this is new to me, so please excuse me if I'm missing something obvious. I'm trying to create a Custom Module of some user profile data. Upon trying to publish the item via the given Sitefinity code (dynamicModuleManager.Lifecycle.Publish(userProfileItem);), I get an error telling me that:
"Object references between two different object scopes are not allowed. The object 'Telerik.Sitefinity.DynamicTypes.Model.Profiles.UserProfile' is already managed by 'ObjectScopeImpl 0x60 OpenAccessRuntime.EnlistableObjectScope' and was tried to be managed again by 'ObjectScopeImpl 0x67 OpenAccessRuntime.EnlistableObjectScope'."
Does anyone know what this means and/or how to go about resolving it? If I need to include more detail or more of my code, I can gladly do that.
Thanks!
Sitefinity uses teleriks OpenAccess (now Data Access) ORM. So each "Context\Scope" is like a connection or instance of the DB...manages state\etc.
It might help if you paste in your code?
public void PublishUserData(ApplicantFormModel FormData)
var providerName = "OpenAccessProvider";
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type userProfileType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Profiles.UserProfile");
DynamicContent userProfileItem = dynamicModuleManager.CreateDataItem(userProfileType);
// This is how values for the properties are set
userProfileItem.SetValue("Title", "User profile for " + FormData.FirstName + FormData.LastName);
userProfileItem.SetValue("FirstName", FormData.FirstName);
userProfileItem.SetValue("LastName", FormData.LastName);
userProfileItem.SetValue("KlickID", FormData.APIReturnedID);
userProfileItem.SetValue("Email", FormData.Email);
userProfileItem.SetValue("Phone", FormData.Phone);
userProfileItem.SetString("UrlName", "url.com/profile/" + FormData.LastName);
userProfileItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
userProfileItem.SetValue("PublicationDate", DateTime.Now);
userProfileItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();
// We can now call the following to publish the item
dynamicModuleManager.Lifecycle.Publish(userProfileItem);
//You need to set appropriate workflow status
userProfileItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");
// Now the item is published and can be seen in the page
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();
Hello Matt,
You should not have issues creating an item using the code you have provided. Most probably the problem you are observing is caused by the fact that the object is used in two transactions at the same time. It is really hard to say what might be causing the issue.
Just for a test can you please try to instantiate the dynamic module manager using the following code:
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(String.Empty);
Hi,
Thanks for the info. I tried instantiating the dmm using String.Empty as you suggested and I'm still getting the same double scoping error. Any other ideas?
Thanks!
Hello Matt,
This issue might be caused if you have instantiated a manager and have not saved the changes between two different operations. Thus, the managers works in different scope, which is not allowed in OpenAccess.
The other reason for the issue might be if you are not authenticated in the thread you wish to update. The dynamicModuleManager.Lifecycle.Publish() method requires authentication. In order to allow anonymous users to create items without the need to authenticate, you can wrap your code (related to creating and saving the item) in:
using
(
new
ElevatedModeRegion(dynamicModuleManager))
// your code goes here