Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

To Browse and upload file using dialog box

(1) ShareShare
ReportReport
Posted on by 1,457
hi 
i need to add one button on form and when i click the that button a dialog should open which should browse and upload file in any format how can i get this done i have can any one plz guide me on this .
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To Browse and upload file using dialog box
    hi ,
    martin 
    thanks for helping me out,
    from yesterday i am trying to verify the comments , but the page is going into infinite loding , i will verify soon when this issue get fixed.
    thanks ,
    regards,
    Dinesh.
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    To Browse and upload file using dialog box
    I assume that you're using SharePoint to store attachments and the error comes from attachFile(). It's unrelated to the topic of this this thread (To Browse and upload file using dialog box), therefore if you need some help with it, please create a new thread (with an appropriate title). A good idea will be preparing a sample code without involving any form.
     
    It seems that you're now able "To Browse and upload file using dialog box", therefore you should probably mark the replies solving your problem(s).
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    To Browse and upload file using dialog box
    I wonder why you ask me how to set the parameter when you already wrote code for it. Here is it:
    DocuRef docuRef = DocumentManagement::attachFile(
                            DTProrpertyHeader.TableId,
                            DTProrpertyHeader.RecId,
                            ...
    The bug is somewhere else. As I told you, you forgot to put any value to dtProrpertyHeader and therefore it's empty.
     
    Regarding Data Source property, you don't necessarily set it in some situations, it's safer to do it and that's why I explicitly told you to do it.
     
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To Browse and upload file using dialog box
     hi ,
    i am getting the value , of "!_refRecId " , made some changes in code ,  i am getting this error now , i will continue debugging let you know . 
    : 'Unable to create a communication channel with the SharePoint server 'ehecloud.sharepoint.com'.'
    final class DTPropertyfileUpload  
    {
        public static void main(Args _args)
        {
            FileUploadTemporaryStorageResult  result;
            DTProrpertyHeader        dtProrpertyHeader;
    
    
            dtProrpertyHeader = _args.record();
     
            result = File::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy));
            if(result.getUploadStatus())
            {
                str fileName = "YourFileName.jpeg"; //Insert the name of the attachchment
    
                using(System.IO.MemoryStream stream = result.openResult() as System.IO.MemoryStream)
                {
                    DocuRef docuRef = DocumentManagement::attachFile(
                                dtProrpertyHeader.TableId,
                                dtProrpertyHeader.RecId,
                                dtProrpertyHeader.DataAreaId,
                                DocuType::typeImage(),
                                stream,
                                fileName,
                                null,
                                fileName);
                }
            }
     
        }
    
    }
    thanks, 
    Dinesh
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To Browse and upload file using dialog box
    hi ,
    martin , thanks for letting me know to debug attachfile method , the value of "!_refRecId " is 0 here , can you please tell me hou can i pass value there .
      and in previous comment you told me 
    " Then you can simply drag the menu item to a form. This allows you to use both the logic and the menu item (including the label and security permissions). In the form, you can set the Data Source property and access the record in main() by _args.record()."
     do i have to set the data  source property of action menuitem or menu item button . to get args. record.



    thanks ,
    dinesh .
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    To Browse and upload file using dialog box
    You need to learn how to debug code, because that's an important part of developer's job.
     
    You called DocumentManagement::attachFile() and it threw an error. Your next step shouldn't be asking strangers on internet; instead, look at the why the error occurred. Begin by opening the definition of attachFile(). At the very top, you'll see this code:
    if (!_refTableId || !_refRecId || !_refDataAreaId || !_type || !_attachmentName || _file == null || !_fileName)
    {
        throw error(error::missingParameter(null));
    }
    This is cause - you call the method with one - or more - parameters empty.
     
    The next is finding which parameter is empty. That can be done either by using the debugger, or by reviewing your code. You can easily notice that you made the most common mistake of yours - you declared a variable (dtProrpertyHeader) and completely forgot to put any value there. You'd immediately see the bug if you didn't completely forget to debug your code.
     
    You're wasting time of both of us by not doing any attempt to debug your code. Instead of simply reviewing your code or running it in debugger, you wrote down a forum reply and keep waiting for a reply...
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To Browse and upload file using dialog box
    i have put the code on main() to upload attachment 
    final class DTPropertyfileUpload  
    {
        public static void main(Args _args)
        {
            FileUploadTemporaryStorageResult  result;
            DTProrpertyHeader        dtProrpertyHeader;
     
            result = File::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy));
            if(result.getUploadStatus())
            {
                str fileName = "YourFileName.jpeg"; //Insert the name of the attachchment
    
                using(System.IO.MemoryStream stream = result.openResult() as System.IO.MemoryStream)
                {
                    DocuRef docuRef = DocumentManagement::attachFile(
                                DTProrpertyHeader.TableId,
                                DTProrpertyHeader.RecId,
                                DTProrpertyHeader.DataAreaId,
                                DocuType::typeImage(),
                                stream,
                                fileName,
                                null,
                                fileName);
                }
            }
     
        }
    
    }
    but i am getting error here , and at the time of devlopement of form i  by mistake put the name Prorperty instead of Property , i have assign this class to menuitem and which will call by menu item button . can you please guide me on this .
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    To Browse and upload file using dialog box
    If dtProrpetyHeader is null, it means that form doesn't contain such a data source. Are you sure that you have the typo (Prorperty instead of Property) in the data source name?  You can find these problems faster if you don't make the mistake of hard-coding objects names as strings. Instead, use methods like formDataSourceStr().
     
    Do you need an event handler at all? If it's a custom form, you could put the logic directly to clicked(), instead of using an event handler. Except of the fact that clicked() isn't a good place for business logic. When you put code directly to the form, or to an extension, you can refer to the data source simply by name. Using an event handler makes things more complicated.
     
    Another approach is putting the logic to a class (with main() method) and creating an action menu item. Then you can simply drag the menu item to a form. This allows you to use both the logic and the menu item (including the label and security permissions). In the form, you can set the Data Source property and access the record in main() by _args.record().
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To Browse and upload file using dialog box
     i am getting the object refrence is not set to an instance of object error .

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans