첨부 파일이 있는 목록 항목을 SharePoint 2003에서 SharePoint 2007의 기존 목록으로 전송
-
05-07-2019 - |
문제
SharePoint 2003 사이트의 목록에 2007 사이트의 기존 목록에 추가해야 하는 항목이 있습니다.항목에 첨부 파일이 있습니다.
PowerShell 또는 C#을 사용하여 이를 어떻게 수행할 수 있나요?
해결책
저는 이를 달성하기 위해 SharePoint 웹 서비스와 함께 C# 프로그램을 사용하게 되었습니다.Eric White의 블로그에서도 확장 메서드(GetXElement, GetXmlNode)를 사용했습니다. 여기 SharePoint의 XML을 더 쉽게 작업할 수 있게 해주는 XMLNodes와 XElements 간을 변환합니다.
다음은 첨부 파일을 포함한 목록 데이터를 하나의 SharePoint 목록(2003 또는 2007)에서 다른 목록으로 전송하는 데 필요한 대부분의 코드에 대한 템플릿입니다.
1) 새 항목이 대상 목록에 추가된 후 첨부 파일을 이동하는 코드입니다.
// Adds attachments from a list item in one SharePoint server to a list item in another SharePoint server.
// addResults is the return value from a lists.UpdateListItems call.
private void AddAttachments(XElement addResults, XElement listItem)
{
XElement itemElements = _listsService2003.GetAttachmentCollection(_listNameGuid, GetListItemIDString(listItem)).GetXElement();
XNamespace s = "http://schemas.microsoft.com/sharepoint/soap/";
var items = from i in itemElements.Elements(s + "Attachment")
select new { File = i.Value };
WebClient Client = new WebClient();
Client.Credentials = new NetworkCredential("user", "password", "domain");
// Pull each attachment file from old site list and upload it to the new site list.
foreach (var item in items)
{
byte[] data = Client.DownloadData(item.File);
string fileName = Path.GetFileName(item.File);
string id = GetID(addResults);
_listsService2007.AddAttachment(_newListNameGuid, id, fileName, data);
}
}
2) 이전 SharePoint 목록을 반복하고 새 목록을 채우는 코드입니다.
private void TransferListItems()
{
XElement listItems = _listsService2003.GetListItems(_listNameGuid, _viewNameGuid, null, null, "", null).GetXElement();
XNamespace z = "#RowsetSchema";
foreach (XElement listItem in listItems.Descendants(z + "row"))
{
AddNewListItem(listItem);
}
}
private void AddNewListItem(XElement listItem)
{
// SharePoint XML for adding new list item.
XElement newItem = new XElement("Batch",
new XAttribute("OnError", "Return"),
new XAttribute("ListVersion", "1"),
new XElement("Method",
new XAttribute("ID", "1"),
new XAttribute("Cmd", "New")));
// Populate fields from old list to new list mapping different field names as necessary.
PopulateFields(newItem, listItem);
XElement addResults = _listsService2007.UpdateListItems(_newListNameGuid, newItem.GetXmlNode()).GetXElement();
// Address attachements.
if (HasAttachments(listItem))
{
AddAttachments(addResults, listItem);
}
}
private static bool HasAttachments(XElement listItem)
{
XAttribute attachments = listItem.Attribute("ows_Attachments");
if (System.Convert.ToInt32(attachments.Value) != 0)
return true;
return false;
}
3) 이 샘플에 대한 기타 지원 코드입니다.
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Net;
using System.IO;
// This method uses an map List<FieldMap> created from an XML file to map fields in the
// 2003 SharePoint list to the new 2007 SharePoint list.
private object PopulateFields(XElement batchItem, XElement listItem)
{
foreach (FieldMap mapItem in FieldMaps)
{
if (listItem.Attribute(mapItem.OldField) != null)
{
batchItem.Element("Method").Add(new XElement("Field",
new XAttribute("Name", mapItem.NewField),
listItem.Attribute(mapItem.OldField).Value));
}
}
return listItem;
}
private static string GetID(XElement elem)
{
XNamespace z = "#RowsetSchema";
XElement temp = elem.Descendants(z + "row").First();
return temp.Attribute("ows_ID").Value;
}
private static string GetListItemIDString(XElement listItem)
{
XAttribute field = listItem.Attribute("ows_ID");
return field.Value;
}
private void SetupServices()
{
_listsService2003 = new SPLists2003.Lists();
_listsService2003.Url = "http://oldsite/_vti_bin/Lists.asmx";
_listsService2003.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
_listsService2007 = new SPLists2007.Lists();
_listsService2007.Url = "http://newsite/_vti_bin/Lists.asmx";
_listsService2007.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
}
private string _listNameGuid = "SomeGuid"; // Unique ID for the old SharePoint List.
private string _viewNameGuid = "SomeGuid"; // Unique ID for the old SharePoint View that has all the fields needed.
private string _newListNameGuid = "SomeGuid"; // Unique ID for the new SharePoint List (target).
private SPLists2003.Lists _listsService2003; // WebService reference for the old SharePoint site (2003 or 2007 is fine).
private SPLists2007.Lists _listsService2007; // WebService reference for the new SharePoint site.
private List<FieldMap> FieldMaps; // Used to map the old list to the new list. Populated with a support function on startup.
class FieldMap
{
public string OldField { get; set; }
public string OldType { get; set; }
public string NewField { get; set; }
public string NewType { get; set; }
}
다른 팁
아마도 웹 서비스를 통해 무언가를 구현하려고 시도 할 것입니다. 2007 년에 첨부 URL이 ows_attachements 속성에 표시되며 일단 표준 다운로드/업로드를 수행 할 수 있다는 것을 알고 있습니다. 2003 년과 함께 일을 한 지 오래되었지만 새로운 분야라고 생각하지 않습니다.
2003 년부터 올바른 데이터를 얻는 데 어려움이있는 경우 항상 전체 사이트를 2007 년으로 마이그레이션 한 다음 최신 API를 사용하려는 데이터를 추출 할 수 있습니다.
목록을 템플릿으로 저장 한 다음 해당 파일을 2007 포털 템플릿에 저장 한 다음 해당 "사용자 정의"템플릿을 사용하여 새 목록을 작성하려고 했습니까? 첨부 파일과 품목이 총 10MB 이상인 경우에는 작동하지 않으며 2003 년> 2007 년에 작동 할 것이라고 100% 확신하지 못합니다. 10 분 <10 분이 걸리면 시도하지 않으면 시도해 볼 가치가 있습니다. 이미.
이 프로젝트를 만들어야했습니다. http://sourceforge.net/projects/splistcp 비슷한 작업을 위해서는 수정 및 생성 된 시간과 사용자를 유지하기 위해서는 대상에서 로컬 API를 사용해야했습니다. 귀하의 접근 방식의 이점은 2003 년과 2007 년 모두에 대한 지원이 더 쉬운 것으로 보입니다.