Copy Sharepoint pages from one sharepoint server/site to other

Few months ago , I had requirement of copy sharepoint 2007 pages to sharepoint 2013 site in my migration project.And this application is running out of sharepoint server so i have to use sharepoint services.I have used copy-service for this requirement.It will only copy static content of page not other webparts content.

So first fetch source page fieldinfo and bytearray using copy-service ‘GetItem’ method(add copy service reference and programatically change its url).

 

//give source copy-service url and credentials
CopyWS.Copy copyWS = new CopyWS.Copy();
copyWS.Url = "http://wssdev:7001/_vti_bin/copy.asmx";
copyWS.Credentials = new NetworkCredential("sourcesite-username", "sourcesite-password");

//fetch page fieldinfo and bytearray using copy GetItem method from source page.
CopyWS.FieldInformation[] mossPageFieldInfoArray;
byte[] mossPageByteArray;
uint myGetUint = copyWS.GetItem("http://wssdev:7001/Pages/test1.aspx", out mossPageFieldInfoArray, out mossPageByteArray);

 

Now set copy-service url to destination site and copy the page using ‘CopyIntoItems’ method.Pass source page fieldinfo and bytearray into this method.

 

//give destination copy-service url and credentials
copyWS = new CopyWS.Copy();
copyWS.Url = "http://spe2013dev1:4444/_vti_bin/copy.asmx";
copyWS.Credentials = new NetworkCredential("destinationsite-username", "destinationsite-password");

// create page in destination site using source page info.
CopyWS.CopyResult[] copyResultArray;
string[] copyDest = { "http://spe2013dev1:4444/Pages/test1.aspx" };
uint myCopyUint = copyWS.CopyIntoItems("http://wssdev:7001/Pages/test1.aspx", copyDest, mossPageFieldInfoArray, mossPageByteArray, out copyResultArray);

 

Check CopyResult.ErrorCode , if it is success then your page gets copied successfully else it will show error message.

Additional Information:

Page Field Information Array

p3

Page Byte Array
p2

Page Copy Result

p3

Related link:http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems%28v=office.12%29.aspx

Sharepoint 2013 Keywordquery search returns less results then default sharepoint search

I had a requirement in which I want to get the search results programmatically. For this I am using KeyWordQuery to search programmatically. But I am getting less results than default SharePoint search, it is omitting some records (duplicate records) from the results. After spending some time I found one property TrimDuplicates of KeywordQuery object. Set this property to false for fetch all search data.

 

SPSite site = SPContext.Current.Site;
KeywordQuery objkeywordquery = new KeywordQuery(site);
objkeywordquery.QueryText = "Your query goes here";
objkeywordquery.SelectProperties.Add("Field1");
objkeywordquery.SelectProperties.Add("Field2");
objkeywordquery.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
objkeyworkquery.TrimDuplicates = false;
ResultTableCollection queryResults = objkeywordquery.Execute();
ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];
DataTable queryDataTable = new DataTable();
queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);

 

Update Lookup Column Web and List Programmatically

I had requirement of creating custom list programmatically for our varience sites en-us(English) and de-de(German). For that, I have created site-columns, content-type and add site-columns to content-type at root site level. Now I am using same content-type in varience site’s list. But the lookup column of varience site’s list refer to root web ( and list) instead of current web.So to fix this issue I have to manually change the lookup column webid and list of current site not root site.

 

///
/// Set lookup site-columns webid and listid for all variance site
///
public static void SetLookupFieldWebAndListID()
{
  try
  {
    using (SPSite site = new SPSite(RootSiteUrl))
    {
      SPWebCollection allWebs = site.AllWebs;
      foreach (SPWeb currentWeb in allWebs)
      {
        if (PublishingWeb.IsPublishingWeb(currentWeb))
        {
          using (currentWeb)
          {
            //fetch lookup column list and lookup column
            SPList list = currentWeb.Lists["ListName"];
            SPFieldLookup lookupColumn = (SPFieldLookup)list.Fields.GetField("LookupColumnName");

            //update lookup column webid
            lookupColumn.LookupWebId = currentWeb.ID;

            //fetch lookup list
            SPList lookupList = currentWeb.Lists["lookupListName"];

            //update lookup column listid
            if (string.IsNullOrEmpty(lookupColumn.LookupList))
            {
              lookupColumn.LookupList = lookupList.ID.ToString();
            }
            else
            {
              lookupColumn.SchemaXml = lookupColumn.SchemaXml.ReplaceXmlAttributeValue("List", lookupList.ID.ToString()).ReplaceXmlAttributeValue("WebId", currentWeb.ID.ToString());
            }
            lookupColumn.Update(true);
          }
        }

      }
    }

  }
  catch (Exception ex)
  {
    Logger.LogError(ex.Message, "Setup lookup field error", ex.StackTrace);
  }
}

///
/// Relpace the WebID and ListID of lookup field xml-schema.
///
public static string ReplaceXmlAttributeValue(this string xml, string attributeName, string value)
{
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentNullException("xml");
            }
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("value");
            }

            int indexOfAttributeName = xml.IndexOf(attributeName, StringComparison.CurrentCultureIgnoreCase);
            if (indexOfAttributeName == -1)
            {
                throw new ArgumentOutOfRangeException("attributeName", string.Format("Attribute {0} not found in source xml", attributeName));
            }

            int indexOfAttibuteValueBegin = xml.IndexOf('"', indexOfAttributeName);
            int indexOfAttributeValueEnd = xml.IndexOf('"', indexOfAttibuteValueBegin + 1);

            return xml.Substring(0, indexOfAttibuteValueBegin + 1) + value +    xml.Substring(indexOfAttributeValueEnd);
}