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

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

Tags: , , , , ,

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

  1. Hello there! I could have sworn I’ve visited your blog before
    but after browsing through a few of the articles I realized it’s new
    to me. Regardless, I’m certainly pleased I came across it and I’ll be bookmarking it and checking back regularly!

  2. Woah! I’m really digging the template/theme of this blog.

    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between usability and
    visual appearance. I must say that you’ve done a excellent job with this.
    In addition, the blog loads very quick for me on Internet explorer.

    Superb Blog! http://www.yahoo.net

  3. I will right away seize your rss feed as I can not in finding your
    email subscription link or e-newsletter service. Do you’ve any?
    Kindly allow me understand so that I may subscribe.
    Thanks.

  4. Whoa! This blog looks just like my old one! It’s on a
    entirely different subject but it has pretty much the same
    layout and design. Excellent choice of colors!

  5. Ahaa, its fastidious discussion about this post here at
    this blog, I have read all that, so now me also commenting at this place.

  6. I’ve been surfing online more than 4 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. Personally,
    if all web owners and bloggers made good content as you did, the internet will be much more useful than ever before.

  7. I have been surfing online more than 4 hours today,
    yet I never found any interesting article like yours. It
    is pretty worth enough for me. Personally, if all webmasters and bloggers made good content as you did, the web will be
    a lot more useful than ever before.

  8. It is perfect time to make a few plans for the long run and it is time to
    be happy. I’ve read this put up and if I may I desire to suggest you few
    attention-grabbing things or advice. Maybe you could write next articles referring
    to this article. I desire to learn more things approximately it!

  9. Wow! This blog looks just like my old one! It’s on a totally different subject but it has pretty much the same page layout and design.
    Wonderful choice of colors!

Leave a Reply to link 188bet Cancel reply

Your email address will not be published. Required fields are marked *