Clone lists between sites (Client object model)
-
2012年7月30日 下午 02:33
Hello dear colleques,
please - I am just trying to prepare some SP - COM tools for cloning lists from siteSource to SiteDest (SiteDest is Office365 encironment) I'd like to create (or modify if exists) all list on siteDest according siteSource. So, please here are my problems :
1) fields creation doesn't work on siteDest newly created list
public static void CreateListFromOldList(Context conDestList, SP.List listTemplate) { SP.Web oWebsite = conDestList.Web; //create schema info SP.ListCreationInformation inf = new SP.ListCreationInformation(); inf.Title = listTemplate.Title; inf.TemplateType = (int)listTemplate.BaseTemplate; //new list on dest site SP.List newList = oWebsite.Lists.Add(inf); //fields foreach (SP.Field fieldSource in listTemplate.Fields) { SP.Field fnew = newList.Fields.AddFieldAsXml(fieldSource.SchemaXml, true, SP.AddFieldOptions.DefaultValue); //new SP.Field(condestList.clientContext,newList); } newList.Update(); }New list is created OK, but only field Title are stored in list schema .... where the bug is ?
2) is there any possibility to keep List.Id on newly created list the same, as an originla Id ?
Thank you very much, Marian
Marian
所有回覆
-
2012年7月31日 下午 02:12
Hello!
1) In your foreach, try to call fnew.Update();
2) SharePoint doesn't allow to set desirable id of the list. So getting list in your code, use list's title or list's url (the using of url is more reliable). To learn more you can read, for example, this blogpost - SharePoint: Getting a SPList with no exceptions to be thrown.
.Net Follower (http://dotnetfollower.com)
- 已編輯 .Net Follower 2012年7月31日 下午 02:20
- 已標示為解答 Marian__ 2012年8月1日 下午 01:02
-
2012年8月1日 上午 09:00
Hello,
1) still does't work :( I will try to do some more tests... Or have you a small code snippet please ?
2) hm, when i tried some 3rd-party tools, there was a possibility to preserve IDs.
About working with "title" or "url" - my suggestion is not to use it; because these attribs can be overwritten by user. The only relevant attrib is ID. Or I am wrong ?
new 3) please do you know - is there some "Client object model" library (or extensions) more friendly for developers ?
thank you very much
Marian
- 已標示為解答 Marian__ 2012年8月1日 下午 01:02
-
2012年8月1日 上午 11:32
Hi Marian,
The issue is that you have not added the fields to the default view. You need to add the fields to the default view too.
SPView defaultView = list.Views["ViewName"]; defaultView.ViewFields.Add(ColumnName); defaultView.Update();
Answer to your second question- No, you cannot preserve the list id. It is generated by SharePoint.
Let me know if you still face any issue.
Ashish Mishra|MCTS,MCPD in SharePoint 2010| Twitter:https://twitter.com/#!/707Mishra| Blog: http://www.sharepointblog.co.uk/author/ashishmishra/
- 已編輯 Ashish Mishra31 2012年8月1日 下午 12:02
- 已標示為解答 Marian__ 2012年8月1日 下午 01:01
-
2012年8月1日 上午 11:48
Hello!
SharePoint allows to preserve IDs of everything from a list's schema (fields, views, content types), but not the ID of the list itself. So, if you get the list using its ID, you are bound to the certain environment, and your code won't work somewhere else. That is why I recommend use the list's url, which is (from my experience) rarely changed in comparison with the list's title..Net Follower (http://dotnetfollower.com)
- 已標示為解答 Marian__ 2012年8月1日 下午 01:01
-
2012年8月1日 下午 12:43
Hello Ashis,
thank you for answer - I know, that I have to newly added fields bind to view, that's ok. (I test operation in New form. aspx, which respect list changes...) But if there is nothing - nothing can be addedd to view. :(
I think - the reason of my errors (or corrupted results after operations) is that I am not working correctly with clinet context. My idea was to keep all Tool lifecycle open static object "_client context". But after ExecuteQuery() and _client.context.Load(...) method calling from my Tool maybe, result from operations are "strange"....
So I must focus how to handle clinetContext lifecycle, becaude maybe thats the place, from where my errors come.
If you have some other suggestions, please let me know.
Marian
- 已標示為解答 Marian__ 2012年8月1日 下午 01:01
-
2012年8月1日 下午 01:01
Good afternoon,
I will polemize with you about a question, how to get lists from web objecr:
* Title: no, user can change the title of the list
* List url: no, can be changed , too.
* ID : yes, the only thing, which is permanent after creating.
So my practice tells me to prepare "mapped table" , which is loaded in memory as static object and map columns ListID - ListTitle (or ListUrl). The only thing must be done : to prepare mapped table every time I migrate application out of my developer environment. Thats the reason I'd like to preserver list ID's on new environment : use the same mapped table.
Thank you very much
Marian
-
2012年8月1日 下午 01:47
Hello!
By 'SharePoint allows to preserve IDs of everything from a list's schema' I mean that if you have a schema.xml of a list and deploy it on another environment, all fields, views, content types will have the same IDs they had on the previous environment, their IDs will be preserved :) But, of course, you can change them through Object Model at any time. Unlike, SP lists can't have the same ids on different environments. So, writing code for my project, I use url (last part of url to be more precise) to get lists. Users usually don't care about lists urls, but they ask to change lists titles from time to time. Your "mapped table", I believe, is a valid solution too, but requires some additional things to do.
.Net Follower (http://dotnetfollower.com)
- 已標示為解答 Marian__ 2012年8月1日 下午 02:17
-
2012年8月1日 下午 02:17
Hi, totaly agree.
Thanks for your help , nice day to everybody.
Marian

