+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Copy an Image from one library to another?

Copy an Image from one library to another?

6 posts, 0 answered
  1. Brandon
    Brandon avatar
    16 posts
    Registered:
    23 Oct 2008
    22 Feb 2011
    Link to this post
    Hello,
      I need to copy an Image from one Library to another programmatically.  Here's what I have so far to cycle through the image libraries - basically creating libraries based on category (for archiving).  What I need to do is:

    1> Check to see if cntPhoto exists in Archive Library - I guess I can match names.
    2> If it does not exist duplicate it into it.  I see how I could use GetMetaData() and .CreateContent() or maybe .UploadFile() to get all the fields, but what holds the actual picture byte[]?

    Thanks,
    Brandon


     

    IList listOfAllLibraries = libraryManager.GetAllLibraries();
                    if (listOfAllLibraries.Count > 0)
                    {
      
                        foreach (Telerik.Libraries.ILibrary lib in listOfAllLibraries)
                        {
                            if (lib.TypeName == "Image")
                            {
                                foreach (IContent cntPhoto in lib.GetItems())
                                {
                                    if (cntPhoto.GetMetaData("Category").ToString() != "")
                                    {
                                        Telerik.Libraries.ILibrary theArchive = libraryManager.GetLibrary(cntPhoto.GetMetaData("Category").ToString());
                                        if (theArchive == null)
                                        {
                                            theArchive = libraryManager.CreateLibrary();
                                            theArchive.Name = cntPhoto.GetMetaData("Category").ToString();
                                            libraryManager.SaveLibrary(theArchive);
                                        }
      
                                        //NEED TO COPY cntPhoto INTO theArchive 
                                    }
                                }
                            }
                        }
  2. Brandon
    Brandon avatar
    16 posts
    Registered:
    23 Oct 2008
    22 Feb 2011
    Link to this post
    Ok, I've found most of what I need - however when I do the following code - the libraries are created, and in Images and Documents they show up along with the proper number of images.  However, when you actually go into the Library there is no content. 

    So for example - I will Click on Modules->Images and Documents.  I have it in List mode.  It tells you how many items are in that library.  In my case 2.  I think click on the Library to see the files in it, none show and the 'Click to Add First Item' link is there.  So I must be missing something somewhere.

    Thanks,
    Brandon

    IList listOfAllLibraries = libraryManager.GetAllLibraries();
                   if (listOfAllLibraries.Count > 0)
                   {
                       foreach (Telerik.Libraries.ILibrary lib in listOfAllLibraries)
                       {
                           if (lib.TypeName == "Image")
                           {
                               foreach (IContent cntPhoto in lib.GetItems())
                               {
                                   if (cntPhoto.GetMetaData("Category").ToString() != "")
                                   {
                                       Telerik.Libraries.ILibrary theArchive = libraryManager.GetLibrary(cntPhoto.GetMetaData("Category").ToString());
                                       if (theArchive == null)
                                       {
                                           theArchive = libraryManager.CreateLibrary();
                                           theArchive.Name = cntPhoto.GetMetaData("Category").ToString();
                                           theArchive.TypeName = "Image";
                                           libraryManager.SaveLibrary(theArchive);
                                       }
                                       
                                       //Check to see if it exists
                                       bool bExists = false;
                                       IContent cntArchivePhoto = null;
                                       foreach (IContent ca in theArchive.GetItems())
                                       {
                                           if (ca.GetMetaData("Name") == cntPhoto.GetMetaData("Name"))
                                           {
                                               bExists = true;
                                               cntArchivePhoto = ca;
                                               break;
                                           }
                                       }
                                       if (!bExists)
                                       {
                                           //Create it
                                           cntArchivePhoto = libraryManager.CreateContent("image/jpeg");
                                           cntArchivePhoto.ParentID = theArchive.ID;
                                           cntArchivePhoto.Content = cntPhoto.Content;
                                       }
                                       IDictionary<string, IMetaInfo> metakeys = cntManager.Provider.GetMetaKeys(cntPhoto);
                                       foreach (string key in metakeys.Keys)
                                       {
                                           cntArchivePhoto.SetMetaData(key, cntPhoto.GetMetaData(key));
                                       }
                                       libraryManager.SaveContent(cntArchivePhoto);
                                   }
                               }
                           }
                       }
  3. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    22 Feb 2011
    Link to this post
    Hello Brandon,

    Please clear the browser cache and check whether there is are record for the items you have created inside

    [sf_CmsBinaryContent] , [sf_GCMetaData] and [sf_CmsContentBase]

    Since you call SaveContent of the manager the items should be created.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  4. Brandon
    Brandon avatar
    16 posts
    Registered:
    23 Oct 2008
    22 Feb 2011
    Link to this post
    Yes - I found the ID in sf_CMSContentBase of an item with a url of ~/libraries/archivetest1/nameofphoto (which means it's in the archivetest1 library which is one of the dynamically created libraries).

    I then took that ID and matched it on the ContentID of sf_GCMetaData and also to ID of sf_CmsBinaryContent.  It had a record in all 3 tables.

    I attached Screenshots of the results.  This was after I cleared the cache (and actually in IE instead of FIreFox, so a completely different browser as well)

    Thanks,
    Brandon

    Table Values:
    sf_CmsContentBase
    /Libraries  62594148-88ae-44b1-8b02-da30361b4dc7    image/jpeg  8713a07a-d70e-42c8-b60f-2741d8411eef    ~/Libraries/ArchiveTest1/Chrysanthemum  ~/libraries/archivetest1/chrysanthemum  0   NULL    admin

    sf_GCMetaData
    62594148-88ae-44b1-8b02-da30361b4dc7    127 AlternateText   /Libraries  0       NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Author  /Libraries  0   5   NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Category    /Libraries  0   ArchiveTest1    NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Description /Libraries  0       NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Extension   /Libraries  0       NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Height  /Libraries  3   NULL    NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Name    /Libraries  0   Chrysanthemum   NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 PermaLink   /Libraries  0       NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Size    /Libraries  3   NULL    NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    62594148-88ae-44b1-8b02-da30361b4dc7    127 Width   /Libraries  3   NULL    NULL    NULL    0   0   NULL    False   NULL    1   8713a07a-d70e-42c8-b60f-2741d8411eef
    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

    sf_CmsBinaryContent
    /Libraries 62594148-88ae-44b1-8b02-da30361b4dc7 127 <Binary data>
    /Libraries  62594148-88ae-44b1-8b02-da30361b4dc7    127 <Binary data>
  5. Brandon
    Brandon avatar
    16 posts
    Registered:
    23 Oct 2008
    23 Feb 2011
    Link to this post
    Made some more progress -
    The initital copy does not set height/width/size or extension.  If I manually set those in sf_GCMetaData then it sort of shows up in the library detail view.  Attached is a screenshot of results.  If I click on Edit->View Details the image is there.  However the preview is missing.  Any ideas on what else I need to set or copy over?

    Thanks,
    Brandon
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    28 Feb 2011
    Link to this post
    Hello Brandon,

    Could you check the value that are set for the metakeys in libraryManager.SaveContent(cntArchivePhoto); . You can do this in immediate window of Visual Studio. I see that you save the metakeys, so I am  not sure how they are not persisted in the metadat table. Also verify the .Content of the item.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Register for webinar
6 posts, 0 answered