0
votes

Our company’s main site has been using Sitefinity for over a year now and we have one custom widget that uses a module created via module builder programmatically with a new custom widget in development. For both widgets we have recently started sporadically receiving the following error:

Violation of PRIMARY KEY constraint ‘pk_sf_dynamic_content’. Cannot insert duplicate key in object ‘dbo.sf_dynamic_content’. The duplicate key value is (581f9be9-f9b8-6ac8-bf63-ff000000686f). The statement has been terminated.

We haven’t been able to find a pattern in when it happens. Is Sitefinity doing something to cause this? We've verified that it isn't being called twice. As a last resort, manually setting the id on the item didn't work.

Here's the snippet of code that causes the exception to be thrown:

DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(String.Empty);

Type downloadType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Resources.Download");
DynamicContent item = dynamicModuleManager.CreateDataItem(downloadType);

var downloadID = Guid.NewGuid().ToString();
item.SetValue("DownloadID", downloadID);
item.SetValue("DateDownloaded", DateTime.Now);

item.SetString("UrlName", downloadID);
item.SetValue("Owner", SecurityManager.GetCurrentUserId());
item.SetValue("PublicationDate", DateTime.Now);
item.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");

// Attach User & Resource
var userType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Resources.User");
var userItem = dynamicModuleManager.GetDataItems(userType).FirstOrDefault(d => d.GetValue<string>("UserID").ToString().Equals(user.UserID) && d.Status == ContentLifecycleStatus.Master);
if (userItem != null)
{
    item.CreateRelation(userItem, "User");
}

var resourceType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Resources.Resource");
var resourceItem = dynamicModuleManager.GetDataItems(resourceType).FirstOrDefault(d => d.GetValue<string>("ResourceID").ToString().Equals(resource.ResourceId) && d.Status == ContentLifecycleStatus.Master);
if (resourceItem != null)
{
    item.CreateRelation(resourceItem, "Resource");
}

// Save Download
dynamicModuleManager.Provider.SuppressSecurityChecks = true;
dynamicModuleManager.SaveChanges(); //<== exception here
1

1 Answers

0
votes

This is typically due to more than one item with the same urlname. This is a unique identified for sitefinty or the title field. Do a look up on this before you create the item to insure uniqueness.