Kentico 7控制台应用程序

时间:2016-03-24 01:04:54

标签: c# kentico

类型' System.NullReferenceException'的第一次机会异常。在代码

中到达此行时发生CMS.GlobalHelper.dll错误
// Save to database
node.Update();

代码中的所有其他行都包含值。代码需要更新其中一个页面上的NodeAliasPath

// Create an instance of the Tree provider first
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

//Gets all the nodes that have the node alias path of Dinning-Commons
var nodes = tree.SelectNodes("SietName", "%contains part of nodealispath%", "en-us", false);        

if (nodes != null)
{
    // Loop through all documents
    foreach (TreeNode node in nodes)
    {

        Console.WriteLine("NodeAliasPath before change  " + node.NodeAliasPath);
        file.WriteLine("              ");
        file.WriteLine("NodeAliasPath before change  " + node.NodeAliasPath);
        Console.WriteLine("              ");
        file.WriteLine("              ");        

        String newNodeAliasPath = node.NodeAliasPath.Replace("old node alias path", "new node alias path);    

        //Set the value this way
        bool returnNodeValue = node.SetValue("NodeAliasPath", newNodeAliasPath);
Console.WriteLine(" returnNodeValue " + returnNodeValue);        

        //Or set value thos way
        Console.WriteLine(" return value from  Set Value -- NodeAlias Path  " + node.SetValue("NodeAliasPath", newNodeAliasPath));
        Console.WriteLine("    __________________________      ");
        file.WriteLine(" return value from  Set Value -- NodeAlias Path  " + node.SetValue("NodeAliasPath", newNodeAliasPath));
        file.WriteLine(" __________________________ ");        

        // Save to database----NULL REFERNCE IN CMSHELPER.DLL Here. Code hangs
        node.Update();

1 个答案:

答案 0 :(得分:0)

我想我知道问题是什么 - CMSContext.CurrentUser在控制台应用程序的上下文中为空...用这样的特定用户初始化TreeProvider

UserInfo user = UserInfoProvider.GetUserInfo("administrator");
TreeProvider tree = new TreeProvider(user);

另一个原因可能是您正在尝试更改NodeAliasPath。移动文档并非易事。我建议使用TreeProvider.MoveNode()DocumentHelper.MoveDocument()来确保数据库保持一致。