/// <summary>
        /// Runs the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="keyValues">The key values.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override int Execute(string command, StringDictionary keyValues, out string output)
        {
            output = string.Empty;

            string url = Params["url"].Value;
            SetWebPartStateAction action = SetWebPartStateAction.Update;

            if (Params["open"].UserTypedIn)
            {
                action = SetWebPartStateAction.Open;
            }
            else if (Params["close"].UserTypedIn)
            {
                action = SetWebPartStateAction.Close;
            }
            else if (Params["delete"].UserTypedIn)
            {
                action = SetWebPartStateAction.Delete;
            }

            string webPartId         = Params["id"].Value;
            string webPartTitle      = Params["title"].Value;
            string webPartZone       = Params["zone"].Value;
            string webPartZoneIndex  = Params["zoneindex"].Value;
            bool   publish           = Params["publish"].UserTypedIn;
            string properties        = Params["properties"].Value;
            string propertiesFile    = Params["propertiesfile"].Value;
            string propertySeperator = Params["propertyseperator"].Value;

            Hashtable props = null;

            if (!string.IsNullOrEmpty(properties) || !string.IsNullOrEmpty(propertiesFile))
            {
                props = Common.WebParts.SetWebPartState.GetPropertiesArray(properties, propertiesFile, propertySeperator);
            }
            Common.WebParts.SetWebPartState.SetWebPart(url, action, webPartId, webPartTitle, webPartZone, webPartZoneIndex, props, publish);

            return((int)ErrorCodes.NoError);
        }
예제 #2
0
        internal static void SetWebPart(string url, SetWebPartStateAction action, string webPartId, string webPartTitle, string webPartZone, string webPartZoneIndex, Hashtable props, bool publish)
        {
            using (SPSite site = new SPSite(url))
                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(url);

                        // 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)
                        {
                            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.
                            }
                        }

                        string  displayTitle            = string.Empty;
                        WebPart wp                      = null;
                        SPLimitedWebPartManager manager = null;
                        try
                        {
                            if (!string.IsNullOrEmpty(webPartId))
                            {
                                wp = Utilities.GetWebPartById(web, url, webPartId, out manager);
                            }
                            else
                            {
                                wp = Utilities.GetWebPartByTitle(web, url, webPartTitle, out manager);
                                if (wp == null)
                                {
                                    throw new SPException(
                                              "Unable to find specified web part using title \"" + webPartTitle + "\". Try specifying the -id parameter instead (use Get-SPWebPartList to get the ID)");
                                }
                            }

                            if (wp == null)
                            {
                                throw new SPException("Unable to find specified web part.");
                            }

                            // Set this so that we can add it to the check-in comment.
                            displayTitle = wp.DisplayTitle;

                            if (action == SetWebPartStateAction.Delete)
                            {
                                manager.DeleteWebPart(wp);
                            }
                            else if (action == SetWebPartStateAction.Close)
                            {
                                manager.CloseWebPart(wp);
                            }
                            else if (action == SetWebPartStateAction.Open)
                            {
                                manager.OpenWebPart(wp);
                            }


                            if (action != SetWebPartStateAction.Delete)
                            {
                                string zoneID    = manager.GetZoneID(wp);
                                int    zoneIndex = wp.ZoneIndex;

                                if (!string.IsNullOrEmpty(webPartZone))
                                {
                                    zoneID = webPartZone;
                                }
                                if (!string.IsNullOrEmpty(webPartZoneIndex))
                                {
                                    zoneIndex = int.Parse(webPartZoneIndex);
                                }

                                manager.MoveWebPart(wp, zoneID, zoneIndex);

                                if (props != null && props.Count > 0)
                                {
                                    SetWebPartProperties(wp, props);
                                }
                                manager.SaveChanges(wp);
                            }
                        }
                        finally
                        {
                            if (manager != null)
                            {
                                manager.Web.Dispose();
                                manager.Dispose();
                            }
                            if (wp != null)
                            {
                                wp.Dispose();
                            }

                            if (checkIn)
                            {
                                file.CheckIn("Checking in changes to page due to state change of web part " + displayTitle);
                            }
                            if (publish && file.InDocumentLibrary)
                            {
                                PublishItems pi = new PublishItems();
                                pi.PublishListItem(file.Item, file.Item.ParentList, false, "Set-SPWebPart", "Checking in changes to page due to state change of web part " + displayTitle, null);
                            }
                        }
                    }
                    finally
                    {
                        if (HttpContext.Current != null && cleanupContext)
                        {
                            HttpContext.Current = null;
                        }
                    }
                }
        }
        internal static void SetWebPart(string url, SetWebPartStateAction action, string webPartId, string webPartTitle, string webPartZone, string webPartZoneIndex, Hashtable props, bool publish)
        {
            using (SPSite site = new SPSite(url))
            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(url);

                    // 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)
                    {
                        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.
                        }
                    }

                    string displayTitle = string.Empty;
                    WebPart wp = null;
                    SPLimitedWebPartManager manager = null;
                    try
                    {
                        if (!string.IsNullOrEmpty(webPartId))
                        {
                            wp = Utilities.GetWebPartById(web, url, webPartId, out manager);
                        }
                        else
                        {
                            wp = Utilities.GetWebPartByTitle(web, url, webPartTitle, out manager);
                            if (wp == null)
                            {
                                throw new SPException(
                                    "Unable to find specified web part using title \"" + webPartTitle + "\". Try specifying the -id parameter instead (use Get-SPWebPartList to get the ID)");
                            }
                        }

                        if (wp == null)
                        {
                            throw new SPException("Unable to find specified web part.");
                        }

                        // Set this so that we can add it to the check-in comment.
                        displayTitle = wp.DisplayTitle;

                        if (action == SetWebPartStateAction.Delete)
                            manager.DeleteWebPart(wp);
                        else if (action == SetWebPartStateAction.Close)
                            manager.CloseWebPart(wp);
                        else if (action == SetWebPartStateAction.Open)
                            manager.OpenWebPart(wp);

                        if (action != SetWebPartStateAction.Delete)
                        {
                            string zoneID = manager.GetZoneID(wp);
                            int zoneIndex = wp.ZoneIndex;

                            if (!string.IsNullOrEmpty(webPartZone))
                                zoneID = webPartZone;
                            if (!string.IsNullOrEmpty(webPartZoneIndex))
                                zoneIndex = int.Parse(webPartZoneIndex);

                            manager.MoveWebPart(wp, zoneID, zoneIndex);

                            if (props != null && props.Count > 0)
                            {
                                SetWebPartProperties(wp, props);
                            }
                            manager.SaveChanges(wp);
                        }

                    }
                    finally
                    {
                        if (manager != null)
                        {
                            manager.Web.Dispose();
                            manager.Dispose();
                        }
                        if (wp != null)
                            wp.Dispose();

                        if (checkIn)
                            file.CheckIn("Checking in changes to page due to state change of web part " + displayTitle);
                        if (publish && file.InDocumentLibrary)
                        {
                            PublishItems pi = new PublishItems();
                            pi.PublishListItem(file.Item, file.Item.ParentList, false, "Set-SPWebPart", "Checking in changes to page due to state change of web part " + displayTitle, null);
                        }

                    }
                }
                finally
                {
                    if (HttpContext.Current != null && cleanupContext)
                    {
                        HttpContext.Current = null;
                    }
                }

            }
        }
예제 #4
0
 public static void SetWebPartById(string url, SetWebPartStateAction action, string webPartId, string webPartZone, string webPartZoneIndex, Hashtable props, bool publish)
 {
     SetWebPart(url, action, webPartId, null, webPartZone, webPartZoneIndex, props, publish);
 }
 public static void SetWebPartByTitle(string url, SetWebPartStateAction action, string webPartTitle, string webPartZone, string webPartZoneIndex, Hashtable props, bool publish)
 {
     SetWebPart(url, action, null, webPartTitle, webPartZone, webPartZoneIndex, props, publish);
 }