Sunday, February 12, 2012

Replace a WebPart with new one in SharePoint

using (SPSite spSiteTest = new SPSite(webURL))
           {
               SPFile file = null;
               using (SPWeb web = spSiteTest.OpenWeb())
               {
                   try
                   {
                       file = web.GetFile(webPageURL);
                       if (file.Level == SPFileLevel.Checkout)
                       {
                           file.CheckIn("Checkin before replace");
                           file.CheckOut();
                       }
                       else
                       {
                           file.CheckOut();
                       }
                       SPLimitedWebPartManager webPartMgr = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                       for (int k = 0; k < webPartMgr.WebParts.Count; k++)
                       {
                           System.Web.UI.WebControls.WebParts.WebPart wpOld = webPartMgr.WebParts[k];
                           if (string.Compare(wpOld.GetType().Name.Trim(), webPartName.Trim(), true) == 0)
                           {
                               //Read old webpart properties
                               string zoneID = webPartMgr.GetZoneID(wpOld);
                               string storageKey = webPartMgr.GetStorageKey(wpOld).ToString();
 
                               //create new webpart object            
                               MyNewWebPart wpNew = new MyNewWebPart();
                               wpNew.Title = wpOld.Title;// "Web Part Title";
                               wpNew.ChromeState = wpOld.ChromeState;// System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
                               wpNew.ChromeType = wpOld.ChromeType;// System.Web.UI.WebControls.WebParts.PartChromeType.None;
 
                               webPartMgr.DeleteWebPart(wpOld);
                               //Add new webpart object to webparts collection     
                               webPartMgr.AddWebPart(wpNew, zoneID, 0);
                               file.Update();
                               web.Update();
                           }
                       }
                   }
                   catch (Exception ex)
                   {
                       if (file != null && file.Level == SPFileLevel.Checkout)
                       {
                           file.UndoCheckOut();
                       }
                   }
                   finally
                   {
                       if (file != null && file.Level == SPFileLevel.Checkout)
                       {
                           file.CheckIn("Added");
                       }
                       web.AllowUnsafeUpdates = false;
                   }
               }
 
           }

Get a List of Site Collections in a Web Application in SharePoint

            

               SPWebApplication webApp = SPWebApplication.Lookup(new Uri(“WebApplication URL”));

               foreach (SPSite site in webApp.Sites)
               {
                   radCmbSiteCollList.Items.Add(new RadComboBoxItem(site.Url));
                   site.Dispose();
               }

Get a List of Web Applications in a SharePoint Farm

Code to iterate through all the web applications in a SharePoint Farm         

              SPFarm farm = SPWebService.ContentService.Farm;
               SPWebApplicationCollection webAppColl = SPWebService.ContentService.WebApplications;
               if (webAppColl != null)
               {
                   foreach (SPWebApplication spWebApp in webAppColl)
                   {
                       lst.Items.Add(new ListItem(spWebApp.AlternateUrls[0].Uri.ToString()));
                   }
                   radCmbWebApplications.Items.Insert(0, new RadComboBoxItem(" -- Select a WebApplication -- ", "-1"));

               }

Access to XMLHttpRequest at 'from origin has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. .net core angular

Issue: The angular application was getting error from API that the origin has been blocked by CORS policy. Solution: Make sure that the...