• Not Answered

Access Sitecore DB content through console application

Hi everyone,

i'm trying to use a console application to access some sitecore items through the database.

I created a console application with visual studio 2015 and i'm trying to access some DB items from of a Sitecore 8.2 release 2 instance

I added all dlls and the App_config of my instance to the bin/debug of my console application

The  Sitecore.Configuration.Factory.GetDatabase("master") works fine but when i try to get an item from the bd with its ID database.GetItem(new Sitecore.Data.ID("{...}"))

 

I get following exception:

An instance of System.Type was null. Additional information: The type was not found in field type declaration "GroupList".

The type initializer for 'Sitecore.Data.Fields.FieldTypeManager' threw an exception.

at Sitecore.Data.Fields.FieldTypeManager.GetFieldType(String name)
at Sitecore.Data.Templates.TemplateField.Builder.SetType(String type)
at Sitecore.Data.Engines.TemplateEngine.AddField(ItemDefinition item, Builder section)
at Sitecore.Data.Engines.TemplateEngine.AddSection(ItemDefinition item, Builder template)
at Sitecore.Data.Engines.TemplateEngine.BuildTemplate(ItemDefinition itemDefinition)
at Sitecore.Data.Engines.TemplateEngine.LoadItemBasedTemplate(ID templateId)
at Sitecore.Data.Engines.TemplateEngine.AddItemBasedTemplates(TemplateDictionary result)
at Sitecore.Data.Engines.TemplateEngine.InternalGetTemplates()
at Sitecore.Data.Engines.TemplateEngine.GetTemplate(ID templateId)
at Sitecore.Data.Managers.TemplateProvider.GetTemplate(ID templateId, Database database)
at Sitecore.Data.Managers.TemplateProvider.GetTemplate(Item item)
at Sitecore.Data.Managers.TemplateProvider.GetTemplateField(ID fieldId, Item item)
at Sitecore.Data.Managers.DefaultTemplateManager.GetTemplateField(ID fieldId, Item item)
at Sitecore.Data.Fields.Field.GetTemplateField()
at Sitecore.Data.Fields.Field.get_Definition()
at Sitecore.Data.Fields.Field.GetSourceItem()
at Sitecore.Data.Fields.Field.GetInheritedValue(Boolean allowStandardValue)
at Sitecore.Data.Fields.Field.GetInheritedValue()
at Sitecore.Pipelines.GetFieldValue.GetInheritedValue.Process(GetFieldValueArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters)
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args)
at Sitecore.Data.Fields.Field.GetValue(Boolean allowStandardValue, Boolean allowDefaultValue, Boolean allowFallbackValue, Boolean allowInheritValue, Boolean allowInnerValue)
at Sitecore.Data.Fields.Field.GetValue(Boolean allowStandardValue, Boolean allowDefaultValue, Boolean allowFallbackValue, Boolean allowInheritValue)
at Sitecore.Data.Fields.Field.GetValue(Boolean allowStandardValue, Boolean allowDefaultValue, Boolean allowFallbackValue)
at Sitecore.Data.Fields.Field.GetValue(Boolean allowStandardValue, Boolean allowDefaultValue)
at Sitecore.Data.Fields.Field.get_Value()
at Sitecore.Data.Items.BaseItem.get_Item(ID fieldID)
at Sitecore.Security.AccessControl.ItemAuthorizationHelper.GetAccessRules(Item item)
at Sitecore.Security.AccessControl.SqlAuthorizationProvider.GetAccessRules(ISecurable entity)
at Sitecore.Security.AccessControl.DefaultAuthorizationManager.GetAccessRules(ISecurable entity)
at Sitecore.Security.AccessControl.ItemSecurity.GetAccessRules()
at Sitecore.Security.AccessControl.ItemAuthorizationHelper.GetItemAccess(Item item, Account account, AccessRight accessRight, PropagationType propagationType)
at Sitecore.Buckets.Security.AuthenticationHelper.GetItemAccess(Item item, Account account, AccessRight accessRight, PropagationType propagationType)
at Sitecore.Security.AccessControl.ItemAuthorizationHelper.GetAccess(Item item, Account account, AccessRight accessRight)
at Sitecore.Security.AccessControl.SqlAuthorizationProvider.GetItemAccess(Item item, Account account, AccessRight accessRight)
at Sitecore.Security.AccessControl.SqlAuthorizationProvider.GetAccessCore(ISecurable entity, Account account, AccessRight accessRight)
at Sitecore.Security.AccessControl.AuthorizationProvider.GetAccess(ISecurable entity, Account account, AccessRight accessRight)
at Sitecore.Security.AccessControl.DefaultAuthorizationManager.GetAccess(ISecurable entity, Account account, AccessRight accessRight)
at Sitecore.Security.AccessControl.DefaultAuthorizationManager.IsAllowed(ISecurable entity, AccessRight right, Account account)
at Sitecore.Security.AccessControl.AuthorizationManager.IsAllowed(ISecurable entity, AccessRight right, Account account)
at Sitecore.Security.AccessControl.ItemAccess.CanRead()
at Sitecore.Data.Managers.ItemProvider.ApplySecurity(Item item, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemProvider.GetItem(ID itemId, Language language, Version version, Database database, SecurityCheck securityCheck)
at Sitecore.ContentTesting.Pipelines.ItemProvider.GetItem.GetItemUnderTestProcessor.Process(GetItemArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters)
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Data.Managers.DefaultItemManager.ExecuteAndReturnResult[TArgs,TResult](String pipelineName, String pipelineDomain, Func`1 pipelineArgsCreator, Func`1 fallbackResult)
at Sitecore.Data.Managers.DefaultItemManager.GetItem(ID itemId, Language language, Version version, Database database, SecurityCheck securityCheck)
at Sitecore.Data.Managers.DefaultItemManager.GetItem(ID itemId, Language language, Version version, Database database)
at Sitecore.Data.Managers.ItemManager.GetItem(ID itemId, Language language, Version version, Database database)
at Sitecore.Data.DefaultDatabase.GetItem(ID itemId)

 

Any ideas or suggestions? 

Thx...

5 Replies

  • Hi Wondambong,

    I’d strongly discourage you going the path of loading DLLs into your console app. You will mostly likely forget to include some of the event handlers (should u choose to update your data) and u will also not utlize data caching of Sitecore.
    It’s much better to access it via WebAPI, depending on which version of Sitecore, either using ItemService doc.sitecore.net/.../sitecoreservicesclient (Sitecore 7.5 and upwards) or Web API - sdn.sitecore.net/.../Documentation.aspx (Sitecore 6.6-7.2). And of course u can always develop your own Web API or web service atop of Sitecore instance the same way u would do for a regular web Apps.

    Cheers,
    Martin
  • In reply to Martin Ivan:

    Hi Martin,

    thanks your answer pointed some critical aspects. I will now explore the ItemService option.

    Does ItemService provide functionality to upload a media file to an existing sitecore item?

    Cheers,
    Wondi
  • In reply to Wondambong Tanessob-Tchinda:

    Hi Wondi,

    What is that you are actually trying to do with console application ?

    Thanks
    Naveed
  • In reply to Naveed Ahmad:

    Hi Naveed,
    I'm trying to programmatically upload(attach) media files to sitecore media items.

    we have media items in the sitecore instance where the media files are missing because of unsuccessfull zip upload.
    so now we have a bunch of sitecore media items without any media file attached. (manual is not an option too much items)

    Thanks...
    Wondi
  • In reply to Wondambong Tanessob-Tchinda:

    Hi Wondi,

    May I suggest that instead of using Console Applications, you can create a custom .aspx utility page, like we have admin/cache.aspx. Just add a new webform page let say at sitecore/admin/imageupload.aspx location, secure it that it is only accessible via admin users and then add your logic to attach image to the media items. You will have access to all of the dlls by default and won't run into such issues. Looks like it is a one time only job. You can copy the aspx page, run your custom logic and then delete it from the CMS server if security is an issue. Have a look at the utitity pages under webroot/sitecore/admin/*.aspx and you will get an idea.

    Thanks
    Naveed