Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 12, 2010 @ 16:28
    Morten Bock
    0

    Media MakeNew throws exception at root

    I am trying to create a folder at the root level of the media section, and it throws a null reference exception. Code is:

    MediaType mediaType = MediaType.GetByAlias("Folder");
    User umbracoUser = User.GetUser(0);
    Media distMediaFolder = Media.MakeNew("folder name", mediaType, umbracoUser, -1);

    This seems to throw a null reference inside the base CMSNode.MakeNew method somewhere, but I can't figure out why.

    Anyone seen this, or know a workaround?

    I am calling the code from an exposed webservice, so there is no currentPage or similar, but I don't see that this should be an issue?

  • Tommy Poulsen 514 posts 708 karma points
    Feb 12, 2010 @ 16:36
    Tommy Poulsen
    0

    Maybe this is a long shot, but I just thought of this one: http://forum.umbraco.org/yaf_postst7538_Error-when-trying-to-create-in-Media-v4.aspx

    >Tommy

     

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 12, 2010 @ 16:55
    Morten Bock
    0

    Well, I'm not using the UI at all, so I shouldn't need the UI xml file to do this.

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 12, 2010 @ 17:04
    Ismail Mayat
    0

    Morten,

    Been caught with this one before you need to do the following when creating at the root you need to do

    Media[] root = Media.GetRootMedias();

    thats how i got it to work.

    Regards

    Ismail

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 12, 2010 @ 17:09
    Morten Bock
    0

    I actually do that earlier in my code. I am looking if a folder already exists in the root, and if not, I want to create it.

    I can loop through the existing nodes just fine. But can't create a new one. How does your complete code look?

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 12, 2010 @ 17:13
    Ismail Mayat
    0

    Morten,

    Doing this in an actionhandler:

     private int createMediaFolder(Document sender)
            {
    
                int id = 0;
                int parentId = 0;
                Media parentMediaFolder;
                try
                {
                    Media[] root = Media.GetRootMedias();
                    Media rootFolder = new Media(Config.Settings.MediaFolderRootID);
                    string parentFolderText = sender.Parent.Text;
    
                    parentId = getExistingFolderID(root, parentFolderText);
    
                    //media folder does not already exist
                    if (parentId == 0)
                    {
                        parentMediaFolder = Media.MakeNew(parentFolderText, new MediaType(1031), UmbracoEnsuredPage.CurrentUser, rootFolder.Id);
                        parentId = parentMediaFolder.Id;
                    }
                    else{
                        parentMediaFolder = new Media(parentId);
                    }
                    if(parentMediaFolder.HasChildren){
                        //does the retailer folder already exist
                        id = getExistingFolderID(parentMediaFolder.Children, sender.Text);
                        if (id == 0) {
                            Media retailerFolder = Media.MakeNew(sender.Text, new MediaType(1031), UmbracoEnsuredPage.CurrentUser, parentId);
                            id = retailerFolder.Id;
                        }
                    }
                    else{
    
                        Media retailerFolder = Media.MakeNew(sender.Text, new MediaType(1031), UmbracoEnsuredPage.CurrentUser, parentId);
                        id = retailerFolder.Id;
                    }
    
                }
                catch (Exception ex)
                {
                    //log
                    errors = true;
                    errorMessage += "\n\n" + ex.Message.ToString();
                    Log.Add(LogTypes.Error, sender.Id, ex.Message.ToString());
                }
                return id;
            }
            private int getExistingFolderID(Media[] root, string folderName)
            {
                int folderID=0;
    
                var folder = (from m in root
                              where m.ContentType.Alias=="Folder" && 
                              m.Text.ToLower() == folderName.ToLower()
                              select m).FirstOrDefault();
    
                folderID = folder != null ? folder.Id : 0;
    
                return folderID;
            } 
  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 12, 2010 @ 18:18
    Morten Bock
    0

    Argh. Found the problem.

    Seems that there was a transaction scope around my code, and umbraco decided to swallow the SQL Exception that the transaction needed to be closed before making the second part of my updates.

    Well, found the cause, so now I just need to figure out how to work around it. Thanks all.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies