Solved

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

Posted on 2010-08-30
4
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 32

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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

617 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