1
votes

I am trying to create new Sitecore item, using Visual Studio solution. But When I call master database I get the error Could Not read Sitecore Configuration. I know this question is already asked but the solution provided is not clear to me.

Can anyone please guide me to exactly which publishing values may get overridden by my web.config file so that I can correct it.

Another solution which is provided is to give edit/modify access for the IIS user on the website folder. It didn't worked for me.

public class MyItem
{
    public void CreateItem()
    {
        using (new Sitecore.SecurityModel.SecurityDisabler())
        {
            Sitecore.Data.Database masterDB = Factory.GetDatabase("master");               
            Item parentItem = masterDB.GetItem(new ID("{140DC116-E743-4C02-9F08-CB73151A5163}"));
            TemplateItem template = masterDB.GetTemplate(new ID("{C9B284A6-0427-4296-8217-E8A3F728D8F0}"));

            parentItem.Add("RanjitAsset1", template);

        }
    }
}
2
Can you add the code you are using to the question? - Mohammed Syam
Yes, here is my code: - Ranjit choudhary
I can't see the code. - Mohammed Syam
Add the code to your original question. Adding code in comments makes it very hard to read. - jammykam
Okay, Can you check your master database connection string name.is it "master"? - Mohammed Syam

2 Answers

0
votes

The problem might occur because your web.config is being overridden by default ASP.net web.config after you publish your solution from VS.

Can you compare your website web.config file to the one from the clean sitecore installation?

0
votes

Andrey is correct. Check the web.config, it might have got overwritten when publishing from Visual Studio.

<site 
        name="website" 
        virtualFolder="/"
        physicalFolder="/" 
        rootPath="/sitecore/content"
        startItem="/home"
        language="en" 
        **database="web"** 
        domain="extranet" 
        allowDebug="true" 
        cacheHtml="true" 
        htmlCacheSize="10MB" 
        enablePreview="true"
        enableDebugger="true" />

If you want to switch the database name, you can do so using the desktop mode. Or, by adding ?sc_content=[database] as a querystring parameter.

Using the API, you can switch the context using SoteContext, and get the Item. Also, use SelectSingleItem method, it looks for an Item in the folder, if exists, it will update it, otherwise, it'll create it.

<!-- language: lang-cs -->  
public class MyItem
{
    public void CreateItem()
    {
        SiteContext targetSiteContext = SiteContext.GetSite(sitename);
        using (var context = new SiteContextSwitcher(targetSiteContext))
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                // do something on the new site context
                var title = @"You title goes here";
                var parentItem = Sitecore.Context.Database.GetItem(new Data.ID("{140DC116-E743-4C02-9F08-CB73151A5163}"));
                var template = Sitecore.Context.Database.GetTemplate(new Data.ID("{C9B284A6-0427-4296-8217-E8A3F728D8F0}"));
                var newItem = Sitecore.Context.Database.SelectSingleItem(parentItem.Paths.Path + "//*[@@name='" + title + "']") ?? template.CreateItemFrom(title, parentItem);
                try
                {
                    newItem.Editing.BeginEdit();
                    newItem.Fields["NewsTitle"].Value = title;
                    //Rest of the fields go here
                    newItem.Editing.AcceptChanges();
                    newItem.Editing.EndEdit();
                }
                catch (Exception ex)
                {
                    Diagnostics.Log.Error("Crawl Error: ", ex);
                    newItem.Editing.CancelEdit();
                }
            }
        }
    }
}