rmicone
asked on
Copying images from one image library to another using object method in WSS 3.0
I am trying to copy list items, specifically images from an Image Library, to another image library list on the same site. I need to do things like create a new img library, set meta data, and copy the items to the new list. My application is written in C# using visual studio 2008, and I have reviewed a lot of material in my books and online docs, and most of them are not specific enough on how to do this.
Do I have to use the web service approach Lists.asmx? Or can I use an object method approach to copy these items to the new list?
I have a book that spells it out using MOSS 2007, which i don't have, just wss 3.0 sp2
basic code outline is once I have the item i want to copy a la... item["Name"] from the SPListItem, how do I copy this to the new list?
Do I have to use the web service approach Lists.asmx? Or can I use an object method approach to copy these items to the new list?
I have a book that spells it out using MOSS 2007, which i don't have, just wss 3.0 sp2
basic code outline is once I have the item i want to copy a la... item["Name"] from the SPListItem, how do I copy this to the new list?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi rmicone,
The URL part of the code isn't important. You may well choose to get references to your SPFile objects in a different way.
The key code for your purposes is the OpenBinary/SaveBinary section, as this will allow you to create the new image instances in the new list.
Remember you will have to write a little extra code to move the metadata over. You'll have to read the property bag and copy that as well as the binary.
Jamie
The URL part of the code isn't important. You may well choose to get references to your SPFile objects in a different way.
The key code for your purposes is the OpenBinary/SaveBinary section, as this will allow you to create the new image instances in the new list.
Remember you will have to write a little extra code to move the metadata over. You'll have to read the property bag and copy that as well as the binary.
Jamie
ASKER
thanks Jaime, I wanted to post my code, though poorly written, just in case it will help anyone else. The whole 'more than one way to do it' here applies, so I was just trying to copy 'folderized' images form one image library/list to another, and getting confused with all the combinations SPFile, SPFolder, SPListItem code snippets there are out there, and finding one that works. In the end I was able to do it by first creating a folder based on a timestamp
string strTimeStamp = System.DateTime.Today.Year .ToString( ) + System.DateTime.Today.Mont h.ToString () + System.DateTime.Today.Day. ToString() + "_" + System.DateTime.Now.Hour.T oString() + System.DateTime.Now.Minute .ToString( ) + System.DateTime.Now.Second .ToString( );
string folderTargetName = "ImgBatch-" + strTimeStamp;
SPWeb web = site.AllWebs[strRelSitePat h];
SPList formsListTarget = web.Lists[listTarget_ListN ame];
SPListItem newFolder = formsListTarget.Items.Add( "", SPFileSystemObjectType.Fol der, folderTargetName);
newFolder.Update()
...
and then once I found the files I wanted to copy to the target List and Folder I did a modified version of your examples:
string strSrcURL = "";
strSrcURL = item["URL Path"].ToString();
SPFileCollection collFilesTarget = null;
collFilesTarget = web.GetFolder(listTarget_L istName + "/" + folderTargetName).Files;
try
{
SPFile sourceFile = web.GetFile(strSrcURL);
//web.AllowUnsafeUpdates = true;
byte[] binFile = sourceFile.OpenBinary();
SPFile targetFile = null;
targetFile = collFilesTarget.Add(strFil eName, binFile);
targetFile.SaveBinary(binF ile);
targetFile.Update();
cntFoundImgs++;
}
catch (System.Exception ex)
{
Session["ErrorMessage"] = ex.Message;
}
yes i know it's ugly code, but hey it works... thanks again!
string strTimeStamp = System.DateTime.Today.Year
string folderTargetName = "ImgBatch-" + strTimeStamp;
SPWeb web = site.AllWebs[strRelSitePat
SPList formsListTarget = web.Lists[listTarget_ListN
SPListItem newFolder = formsListTarget.Items.Add(
newFolder.Update()
...
and then once I found the files I wanted to copy to the target List and Folder I did a modified version of your examples:
string strSrcURL = "";
strSrcURL = item["URL Path"].ToString();
SPFileCollection collFilesTarget = null;
collFilesTarget = web.GetFolder(listTarget_L
try
{
SPFile sourceFile = web.GetFile(strSrcURL);
//web.AllowUnsafeUpdates = true;
byte[] binFile = sourceFile.OpenBinary();
SPFile targetFile = null;
targetFile = collFilesTarget.Add(strFil
targetFile.SaveBinary(binF
targetFile.Update();
cntFoundImgs++;
}
catch (System.Exception ex)
{
Session["ErrorMessage"] = ex.Message;
}
yes i know it's ugly code, but hey it works... thanks again!
ASKER
SPFile sourceFile = sourceWeb.GetFile(fileUrl)
I'm not familiar with your variation tool, but it looks like your assembling a target URL together via
string strDestURL = fileUrl.Replace("/" + _SourceVariationLabel + "/", "/" + varLabel + "/");
That's not a problem I can do that... i'm just going to use a static URL to test... (ex "http://localhost/imglib1/img1.jpg") Which i can get from the item properties of the doc lib item
Then in the target list, I create a binary object
byte[] binFile = sourceFile.OpenBinary();
targetFile = collFiles[strDestURL];
targetFile.SaveBinary(binF
targetFile.Update();
I realize i'm not including your code for checking if it exists, but that's the jist yeah?
At it's core, it will just upload the binary data, and assign the targetFile["Name"] property to be the name from the target URL?
I'm gonna write some test code now, thanks!