More in this section

Forums / Developing with Sitefinity / RadUpload in Products Module

RadUpload in Products Module

9 posts, 0 answered
  1. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    19 Aug 2009
    Link to this post
    I took a look at SelArom's blog regarding adding a link to a video to the News module:

    http://www.selarom.net/blog/2009-07-29/Sitefinity_Linking_Viddler_Videos_and_News_Articles.aspx

    But I would like to take it step further and add a RadUpload to the products module. The RadUpload would create the upload the video to the Libraries provider and then store the GUID as meta field in the product module.

    Can you explain to me where I can add code similar to the code described in the Developers manual here:

    http://www.sitefinity.com/help/developer-manual/libraries-api-contentmanager.html

    in the product module project?

    Also I using the amazon S3. I don't know if that will effect the upload code.

    Thanks in advance.

    bnye
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 Aug 2009
    Link to this post
    Hi bnye,

    Below is a sample code that illustrates how the get the ID of the video file that is being uploaded and set it as a value of a custom metakey.

            LibraryManager libraryManager = new LibraryManager(); 
            ILibrary parentLibrary = libraryManager.GetLibrary("Video"); 
            IContent testContent = libraryManager.CreateContent("audio/x-ms-wmv"); 
            testContent.ParentID = parentLibrary.ID; 
     
            UploadedFile firstFile = RadUpload1.UploadedFiles[0]; 
            byte[] buffer = new byte[firstFile.InputStream.Length]; 
            long position = 0; 
            int currentByte = firstFile.InputStream.ReadByte(); 
            while (currentByte != -1) 
            { 
                buffer[position++] = (byte)currentByte; 
                currentByte = firstFile.InputStream.ReadByte(); 
            } 
            IContent content = null
     
            try 
            { 
                content = libraryManager.UploadFile(buffer, 
                firstFile.GetNameWithoutExtension(), 
                firstFile.GetExtension(), 
                firstFile.ContentType, 
                firstFile.ContentLength, 
                parentLibrary); 
     
                StagedContent temp = libraryManager.GetCurrentState(content.ID) as StagedContent; 
                temp.SetMetaData("Author""Me"); 
                string tempID = temp.ID.ToString(); 
                temp.SetMetaData("MyGuid", tempID); 
     
                      libraryManager.SaveContent(temp, ContentStatus.Published); 
            } 
            catch (ArgumentException ex) 
            { 
                Response.Write("There is an exception"); 
            } 
            testContent.Content = buffer; 
            Response.Write(testContent.GetMetaData("Name") + "<br />"); 
     
     
        } 

    If you want to use the code in a custom module you need to create a view and in its template declare RadUpload control. In this view you should add a button and when clicked it will force the uploading as  the code above illustrates.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    19 Aug 2009
    Link to this post
    You'll have to excuse my ignorance, but I'm still not clear on where to put this code. I have a view ProductEditView.ascx in the Products project under Resources -> ControlTemplates -> Backend, but there is no cs file there. There is a ProductEditView.cs in the Products project under WebControls -> Admin -> Products. Should I put the click event in that file? I guess I'm not sure how to wire up my own events in the Products module project.

    Thanks in advance. 

    bnye
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    20 Aug 2009
    Link to this post
    Hi bnye,

    I have a view ProductEditView.ascx - this is not a view it is a template for a view. Here you can put the button click that will force a post back and saving the data by using the logic from your view ProductEditView.cs.
    In ProductsEditView.cs you can add Control references to the button id under ProductsEditView.ascx.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    20 Aug 2009
    Link to this post
    Then shouldn't the following work in the ProductEditView.cs.

    Thanks.

    bnye

    protected override void InitializeControls(System.Web.UI.Control viewContainer) 
            { 
                base.InitializeControls(viewContainer); 
     
                if (this.TestButton != null
                    this.TestButton.Visible = false
            }  
     
            protected Button TestButton 
            { 
                get 
                { 
                    return base.Container.GetControl<Button>("btnTest"false); 
                } 
            }  

  6. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    20 Aug 2009
    Link to this post
    OK it works now. I had the button inside of the <sf:ContentMetaFields> so it wasn't being picked up at the InitializeControls.

    Thanks for all of your help Ivan.

    bnye
  7. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    21 Aug 2009
    Link to this post
    Ivan -

    It worked for the first upload. However, on all subsequent uploads I get the following error:

    Only new or loaded objects can be joined into a transaction.  
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
     
    Exception Details: Nolics.ORMapper.Base.NolnetException: Only new or loaded objects can be joined into a transaction. 
     
    Source Error:  
     
     
    Line 211:            temp.SetMetaData("MyGuid", tempID);  
    Line 212:  
    Line 213:            libraryManager.SaveContent(temp, ContentStatus.Published);  
    Line 214:          
    Line 215:                
      
     
    Source File: C:\Program Files\telerik\Sitefinity3.6\WebSites\Alighten.SocialContent.Videos\WebControls\Admin\Videos\VideoEditView.cs    Line: 213  
     
    Stack Trace:  
     
     
    [NolnetException: Only new or loaded objects can be joined into a transaction.] 
       Nolics.ORMapper.Base.OdbClassHelper.JoinTransaction(IOdbClass cl, Transaction tran, Transaction& curTransaction, OdbObjectStates& state) +277 
       Telerik.Versioning.Data.LockedLink.Nolics.ORMapper.Base.IOdbClass.Join(Transaction tran) +54 
       Nolics.ORMapper.Base.Transaction.Join(IOdbClass mem) +128 
       Telerik.Versioning.Data.VersionItem.VerifyObject() +425 
       Nolics.ORMapper.Base.OdbClassHelper.VerifyClass(IOdbClass cl) +58 
       Telerik.Versioning.Data.VersionItem.Nolics.ORMapper.Base.IOdbClass.BeginCommit() +28 
       Nolics.ORMapper.Base.Transaction.Commit(Boolean fCleanIfSuccess) +1419 
       Nolics.ORMapper.Base.Transaction.Commit() +34 
       Telerik.Versioning.Data.DefaultProvider.SaveItem(IVersionItem item) +282 
       Telerik.Versioning.VersionManager.SaveItem(IVersionItem item) +47 
       Telerik.Cms.Engine.ContentManager.SaveContent(IContent content, ContentStatus status) +574 
       Alighten.SocialContent.Videos.WebControls.Admin.Videos.VideoEditView.UploadButton_Click(Object sender, EventArgs e) in C:\Program Files\telerik\Sitefinity3.6\WebSites\Alighten.SocialContent.Videos\WebControls\Admin\Videos\VideoEditView.cs:213 
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 
      
     

    Any ideas would be appreciated

    bnye
  8. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    25 Aug 2009
    Link to this post
    I took a look at Gabe's latest webcast on API tips and tricks.


    While there is an interesting piece on re-getting the IPage by calling the manager.getPage multiple times. Do you think this might have something to do with this strange Nolics error that I am getting when I try to upload the video again.

    Thanks for your help.

    bnye

  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 Aug 2009
    Link to this post
    Hi bnye,

    Have you modified some parts of the code I sent you? This error is usually thrown when the content item is not get before the transaction, but I am not able to reproduce it locally with the same solution you received.

    Try using the code below:

       try 
            { 
                content = libraryManager.UploadFile(buffer, 
                firstFile.GetNameWithoutExtension(), 
                firstFile.GetExtension(), 
                firstFile.ContentType, 
                firstFile.ContentLength, 
                parentLibrary); 
     
                // or use LibraryManager.GetCurrentState 
                StagedContent temp = libraryManager.GetStagedContent(content.ID) as StagedContent; 
     
                temp.SetMetaData("Author""Me"); 
                temp.SetMetaData("Guid1", temp.ID); 
     
                libraryManager.SaveContent(temp, ContentStatus.Published); 
            } 


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
9 posts, 0 answered