当前位置: 首页>>代码示例>>C#>>正文


C# WebPart.Dispose方法代码示例

本文整理汇总了C#中System.Web.UI.WebControls.WebParts.WebPart.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# WebPart.Dispose方法的具体用法?C# WebPart.Dispose怎么用?C# WebPart.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.UI.WebControls.WebParts.WebPart的用法示例。


在下文中一共展示了WebPart.Dispose方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetWebPart

        internal static void SetWebPart(WebPart sourceWebPart, string targetUrl, string zone, int? zoneIndex, bool publish, bool test)
        {
            if (sourceWebPart.IsClosed)
            {
                sourceWebPart.Dispose();
                throw new Exception("The source web part is closed and cannot be copied.");
            }

            int zoneIndex1 = sourceWebPart.ZoneIndex;
            if (zoneIndex.HasValue)
                zoneIndex1 = zoneIndex.Value;
            Guid storageKey = Guid.NewGuid();
            string id = StorageKeyToID(storageKey);

            using (SPSite site = new SPSite(targetUrl))
            using (SPWeb web = site.OpenWeb()) // The url contains a filename so AllWebs[] will not work unless we want to try and parse which we don't
            {
                bool cleanupContext = false;
                try
                {
                    if (HttpContext.Current == null)
                    {
                        cleanupContext = true;
                        HttpRequest httpRequest = new HttpRequest("", web.Url, "");
                        HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
                        SPControl.SetContextWeb(HttpContext.Current, web);
                    }

                    SPFile file = web.GetFile(targetUrl);

                    // file.Item will throw "The object specified does not belong to a list." if the url passed
                    // does not correspond to a file in a list.

                    bool checkIn = false;
                    if (file.InDocumentLibrary && !test)
                    {
                        if (!Utilities.IsCheckedOut(file.Item) || !Utilities.IsCheckedOutByCurrentUser(file.Item))
                        {
                            file.CheckOut();
                            checkIn = true;
                            // If it's checked out by another user then this will throw an informative exception so let it do so.
                        }
                    }

                    SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
                    try
                    {
                        string wpTitle = sourceWebPart.Title;
                        if (string.IsNullOrEmpty(wpTitle)) wpTitle = sourceWebPart.DisplayTitle;

                        Logger.Write("Copying web part \"{0}\"...", wpTitle);
                        WebPart newWebPart = (WebPart)Activator.CreateInstance(sourceWebPart.GetType());
                        if (SPCmdletReplaceWebPartType.SetProperties(sourceWebPart, newWebPart, null))
                        {
                            Logger.WriteWarning("An error was encountered setting web part properties so try one more time in case the error is the result of a sequencing issue.");
                            if (SPCmdletReplaceWebPartType.SetProperties(sourceWebPart, newWebPart, null))
                            {
                                Logger.WriteWarning("Unable to set all properties for web part.");
                            }
                        }

                        try
                        {
                            if (!test)
                            {
                                newWebPart.ID = id;

                                manager.AddWebPart(newWebPart, zone, zoneIndex1);
                            }
                        }
                        catch (Exception)
                        {
                            ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };

                            // We've not already added the web part so use the web service to do this.
                            using (WebPartPagesWebService.WebPartPagesWebService svc = new WebPartPagesWebService.WebPartPagesWebService())
                            {
                                // We failed adding via the OM so try the web service as a fall back.
                                svc.Url = manager.Web.Url + "/_vti_bin/WebPartPages.asmx";
                                svc.Credentials = CredentialCache.DefaultCredentials;

                                try
                                {
                                    // Add the web part to the web service.  We use a web service because many
                                    // web parts require the SPContext.Current variables to be set which are
                                    // not set when run from a command line.
                                    StringBuilder sb = new StringBuilder();
                                    XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(sb));
                                    xmlWriter.Formatting = Formatting.Indented;
                                    manager.ExportWebPart(newWebPart, xmlWriter);
                                    xmlWriter.Flush();

                                    svc.AddWebPartToZone(targetUrl, sb.ToString(), Storage.Shared, zone, zoneIndex1);

                                }
                                catch (SoapException ex)
                                {
                                    throw new Exception(ex.Detail.OuterXml);
                                }
                            }
//.........这里部分代码省略.........
开发者ID:GSoft-SharePoint,项目名称:PowerShell-SPCmdlets,代码行数:101,代码来源:SPCmdletCopyWebPart.cs


注:本文中的System.Web.UI.WebControls.WebParts.WebPart.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。