Solved

Copying images from one image library to another using object method in WSS 3.0

Posted on 2010-08-30
4
545 Views
Last Modified: 2013-12-17
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
Comment
Question by:rmicone
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 33562175
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
 
LVL 2

Author Comment

by:rmicone
ID: 33562530
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
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 33562722
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
 
LVL 2

Author Closing Comment

by:rmicone
ID: 33651182
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now