Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 功能停用时删除自定义母版页错误_C#_Sharepoint_Sharepoint 2010_Master Pages - Fatal编程技术网

C# 功能停用时删除自定义母版页错误

C# 功能停用时删除自定义母版页错误,c#,sharepoint,sharepoint-2010,master-pages,C#,Sharepoint,Sharepoint 2010,Master Pages,我有一个功能,在激活时将自定义母版页部署到网站集中的所有站点,并希望在停用时删除自定义母版页的所有跟踪。停用时,将站点母版页设置回v4.master后,尝试删除以前设置为默认值的自定义母版页时发生错误(无法删除文件“custom.master”。错误代码:158)。该功能在出现错误后未完成停用,但大多数文件已被删除,并且品牌已设置回v4.master。再次尝试停用该功能时,会删除最终文件custom.master,不会出错 我不明白少了什么。为什么必须先完成FeatureDeactivating

我有一个功能,在激活时将自定义母版页部署到网站集中的所有站点,并希望在停用时删除自定义母版页的所有跟踪。停用时,将站点母版页设置回v4.master后,尝试删除以前设置为默认值的自定义母版页时发生错误(无法删除文件“custom.master”。错误代码:158)。该功能在出现错误后未完成停用,但大多数文件已被删除,并且品牌已设置回v4.master。再次尝试停用该功能时,会删除最终文件custom.master,不会出错

我不明白少了什么。为什么必须先完成FeatureDeactivating()才能删除custom.master

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
    {
        using (SPWeb web = sitecollection.RootWeb)
        {
            string WebAppRelativePath = sitecollection.ServerRelativeUrl;
            if (!WebAppRelativePath.EndsWith("/"))
            {
                WebAppRelativePath += "/";
            }

            foreach (SPWeb site in sitecollection.AllWebs)
            {
                site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/custom.master";
                site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/custom.master";
                site.UIVersion = 4;
                site.Update();
            }
        }
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
    {
        using (SPWeb web = sitecollection.RootWeb)
        {
            string WebAppRelativePath = sitecollection.ServerRelativeUrl;
            if (!WebAppRelativePath.EndsWith("/"))
            {
                WebAppRelativePath += "/";
            }

            foreach (SPWeb site in sitecollection.AllWebs)
            {
                site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
                site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
                site.UIVersion = 4;
                site.Update();

                WebAppRelativePath = site.Url;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }
                SPFolder folder = web.GetFolder(site.Url + "_catalogs/masterpage/images/");
                if (folder.Exists)
                    folder.Delete();
                folder.Update();

                SPFile file = web.GetFile(site.Url + "_catalogs/masterpage/custom.css");
                if(file.Exists)
                    file.Delete();
                file.Update();

                file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/html5.master");
                if(file.Exists)
                    file.Delete();
                file.Update();

                file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/custom.master");
                if (file.Exists)
                {
                    file.Delete();  // ERROR HAPPENS HERE
                }
                file.Update();

                /*file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/minimal.master");
                if(file.Exists)
                    file.Delete();
                file = web.GetFile("/_layouts/minimal.master");
                if(file.Exists)
                    file.CopyTo(WebAppRelativePath + "_catalogs/masterpage/");

                file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/default.master");
                if(file.Exists)
                    file.Delete();
                file = web.GetFile("/_layouts/default.master");
                if(file.Exists)
                    file.CopyTo(WebAppRelativePath + "_catalogs/masterpage/");*/
            }
        }
    }
}

您可能需要获取对
SPWeb
的新引用,并使用该引用调用
SPWeb.GetFile()


using
块中的
SPWeb
可能仍有对custom.master的引用,需要刷新。

不要从功能的属性中处理SPSite对象。也不要从SPSite.RootWeb处理SPWeb。仅来自SPSite.OpenWeb()。正在尝试执行与OP相同的操作。。。这是否意味着在
SPWeb
使用
块之外,我使用块为新的
SPWeb
创建另一个
?或者我应该在第一个
块中使用
执行此操作?如果我能弄明白或者你能帮我,我会再给你一次投票,使用
块为新的
SPWeb
对象创建一个新的
。如果你想得到更详细的答案,或者它仍然不起作用,请创建一个新问题并发布一些代码,这样你会更加关注你的问题。谢谢-我最终在另一个模块中完成了这项工作(仅在卸载该功能时删除资产,而不是在停用该功能时删除资产)。我的问题是,新的
using
块是在现有的
using
块内部,还是在现有的
using
块之后?可能会把它放在您的答案中以明确(?)我会把它放在使用
块的
之外,因为这样可以更容易地阅读代码并查看发生了什么,而且它还可以确保您不会意外地使用原始的
SPWeb
,而不是新创建的。这实际上可能没有什么区别,但我还没有测试过,最好还是稳妥一点。