Retain internal name of list columns while copying list from One site to another site using client object model

  • Posted by: Softree Technology

Internal name is a property of list fields which is read only and cannot be edited.

Internal name is created according to field title while creating field. If we rename field title internal name will not be changed.

If one user has created a custom column and give title=”Column” then internal name will be “Column”

After renaming field title ‘Internal name’ will be same as previous.
Below Code describes how to retain field internal name while copying or migrating.If field will migrate or Copy from source list to destination list after rename then field in destination list will be created a new Title.

using Microsoft.SharePoint.Client; namespace ConsoleApplication10 {

//this class is used to store data from schema xml

public class FieldInfoInternalName

{

public string InternalName { get; set; }

public string DisplayName { get; set; }

}

class Program

{

static void Main(string[] args)

{

string sourceSiteUrl = "Enter Source Url”;

string destinationSiteUrl = "Enter Destination url";

string sourceListName = "Custom List";

string destinationListName = "New List";

FieldCollection srcFieldColl = null;

using (ClientContext ctx = new ClientContext(sourceSiteUrl))

{

ctx.Credentials = new System.Net.NetworkCredential("UserName", "Password");

Web web = ctx.Web;

ctx.Load(web, w => w.Lists);

List list = web.Lists.GetByTitle(sourceListName);

ctx.Load(list, l => l.Fields.Include(

fld => fld.SchemaXml,

fld => fld.Id,

fld => fld.Title,

fld => fld.Hidden,

fld => fld.ReadOnlyField));

ctx.ExecuteQuery();

//Store all fields from source list in a field collection

srcFieldColl = list.Fields;

}

using (ClientContext destContext = new ClientContext(destinationSiteUrl))

{

destContext.Credentials = new System.Net.NetworkCredential("UserName", "Password");

Web web = destContext.Web;

destContext.Load(web, w => w.Lists);

List list = web.Lists.GetByTitle(destinationListName);

destContext.Load(list, l => l.Fields);

destContext.ExecuteQuery();

for (int iFieldCount = 0; iFieldCount < srcFieldColl.Count; iFieldCount++)

{

Field srcField = srcFieldColl[iFieldCount];

if (!srcField.ReadOnlyField && !srcField.Hidden)

{

string fieldSchemaXml = srcField.SchemaXml;

CreateFieldAsInternalName(ref fieldSchemaXml);

Field field = list.Fields.AddFieldAsXml(fieldSchemaXml, false, AddFieldOptions.DefaultValue);

field.Title = srcField.Title;//Update title

field.Update();

}

}

}

}

private static void CreateFieldAsInternalName(ref string schemaXml)

{

System.Xml.Linq.XDocument xDocument;

string internalName = string.Empty;

string displayName = string.Empty;

try

{

// retrieve internal name and display name from schemaxml

using (System.IO.StringReader reader = new System.IO.StringReader(schemaXml))

{

xDocument = System.Xml.Linq.XDocument.Load(reader);

List<FieldInfoInternalName> FieldMetaInfoSchemaXml =

(

from l in xDocument.Descendants("Field")

select new FieldInfoInternalName

{

InternalName = l.Attribute("StaticName").Value,

DisplayName = l.Attribute("DisplayName").Value

}

).ToList();

foreach (var item in FieldMetaInfoSchemaXml)

{

internalName = item.InternalName;

displayName = item.DisplayName;

}

}

if (displayName.Contains("&"))

displayName = displayName.Replace("&", "&");

if (displayName.Contains("<"))

displayName = displayName.Replace("<", "<");

if (displayName.Contains(">"))

displayName = displayName.Replace(">", ">");

if (displayName.Contains("""))

displayName = displayName.Replace(""", """);

//Replace display name with internal name

if (schemaXml.Contains(displayName) && displayName != internalName)

schemaXml = schemaXml.Replace(displayName, internalName);

}

catch { }

}

}

}

This solution is brought to you by our SharePoint professionals.

Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

e it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Author: Softree Technology

Leave a Reply

18 Comments