Saturday, September 27, 2014

Update Redirect Page url programatically

Below is the code to update the Redirect url of a page in sharepoint.

Code snippet:

SPSite site = new SPSite("http://sharepointsite");
SPWeb web = site.OpenWeb();
SPList library = web.Lists["Pages"];
SPQuery query = new SPQuery();
query.Query = "<<Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>Redirect Page</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='true' /></OrderBy>";
query.RowLimit = 4;
SPListItemCollection items = library.GetItems(query);
foreach (SPListItem item in items)
  {
     if (item.ContentType.Name == "Redirect Page")
         {
              item.File.CheckOut();
              SPFieldUrlValue urlValue = new SPFieldUrlValue();
              urlValue.Description = "https://www.google.com"; // ValidURL is a correct URL that I've tested
              urlValue.Url = "https://www.google.com";
              item["Redirect URL"] = urlValue;
              item.SystemUpdate(false);
              item.File.CheckIn("", SPCheckinType.MajorCheckIn);                  
         }              

  }

No comments:

Post a Comment