• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 598
  • Last Modified:

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?
0
rmicone
Asked:
rmicone
  • 2
  • 2
1 Solution
 
Jamie McAllister MVPSharePoint ConsultantCommented:
There's a code sample here that shows the copying of list file attachments to another list. The fact these are documents and yours are images will make little difference. The treatment of files and action on binaries are the key to it.

http://www.the-north.com/sharepoint/post/Propagating-Document-Library-Items-In-MOSS-Variations-(in-the-same-way-as-Pages).aspx
0
 
rmiconeAuthor Commented:
thanks Jaime, yes so assuming i can get the unique URL of that source img

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(binFile);
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!

0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
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
 
0
 
rmiconeAuthor Commented:
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.Month.ToString() + System.DateTime.Today.Day.ToString() + "_" + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();

        string folderTargetName = "ImgBatch-" + strTimeStamp;

                SPWeb web = site.AllWebs[strRelSitePath];
                SPList formsListTarget = web.Lists[listTarget_ListName];
                SPListItem newFolder = formsListTarget.Items.Add("", SPFileSystemObjectType.Folder, 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_ListName + "/" + folderTargetName).Files;
                                    try
                                    {
                                        SPFile sourceFile = web.GetFile(strSrcURL);
                                        //web.AllowUnsafeUpdates = true;
                                        byte[] binFile = sourceFile.OpenBinary();
                                        SPFile targetFile = null;
                                        targetFile = collFilesTarget.Add(strFileName, binFile);
                                        targetFile.SaveBinary(binFile);
                                        targetFile.Update();
                                        cntFoundImgs++;
                                       
                                    }
                                    catch (System.Exception ex)
                                    {
                                        Session["ErrorMessage"] = ex.Message;
                                    }


yes i know it's ugly code, but hey it works... thanks again!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now