Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-30
4
Medium Priority
?
595 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 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 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 32

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

783 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