Copied to clipboard

Flag this post as spam?

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


  • dcwoodsjr 12 posts 30 karma points
    Aug 09, 2010 @ 20:45
    dcwoodsjr
    0

    Add Generic Properties to Document using documentService web service

    I sucessfully been able to create nodes using the documentservice.asmx web service, but what I've not been able to figure out how to do is to fill the Generic Properties on the new content node that I've created.

    What I've got so far creates the document, but say I've got a Generic Property called 'ServiceBulletin' which is an upload data type where I need to upload a pdf file to it.

    How would I got about uploading the pdf doc into the 'Service Bulletin' property for new document I'm creating.

    This is what I've got so far:

    var document = new UmbracoDocumentService.documentCarrier();
    document.Name = item.Name;
    document.DocumentTypeID = 1071;
    document.Published = true;
    document.ParentID = 1128;
    service.create(document, "username", "password");

    Thanks,

    dw

  • Sascha Wolter 615 posts 1101 karma points
    Aug 10, 2010 @ 12:03
    Sascha Wolter
    0

    Hi dw,

    I'm afraid with the file upload it is a bit more difficult. In the Umbraco backend it is an ASP FileUpload control that's used and Umbraco has it's own implementation to grab that file and put it in the correct directory in the system. If you want to upload a file yourself in the backend you have to populate the 'ServiceBulletin' property yourself with the correct path to the pdf file, yet you would have to program the whole file upload yourself I am afraid (which also means you can't use the documentservice.asmx web service :( ).

    I am just in the process of writing a custom file upload for a new site, it takes a file the user has posted on the frontend and adds a new media item in the backend, here is a bit of code to highlight what I am talking about:

    [I am using the nice Essential Objects AJAX Uploader, it will look a bit different with the standard ASP FileUpload]

            private void UploadFile()
            {
                ...
                if (Page.IsValid)
                {
                    foreach (EO.Web.AJAXPostedFile ajaxPostedFile in AJAXUploader1.PostedFiles)
                    {
                        string tempFileName = ajaxPostedFile.TempFileName;

                        if (File.Exists(tempFileName))
                        {
                            Media newItem = CreateNewMediaItem(ajaxPostedFile.FullClientFileName, tempFileName, ajaxPostedFile.Length);

                            newItem.getProperty("description").Value = txtDescription.Text;

                            //subjects
                            StringBuilder values = new StringBuilder();
                            foreach (ListItem item in checkBoxListSubjects.Items)
                            {
                                if (item.Selected)
                                {
                                    values.Append(values.Length == 0 ? "" : ",");
                                    values.Append(item.Value);
                                }
                            }
                            newItem.getProperty("subjects").Value = values.ToString();
                            //topic
                            newItem.getProperty("topic").Value = ddlTopics.SelectedValue;

                            newItem.Save();
                            newItem.XmlGenerate(new XmlDocument());

                            label.Text = "File successfully uploaded";
                        }
                    }
                }
            }
            private Media CreateNewMediaItem(string fullClientFileName, string sourceFile, int fileLength)
            {
                try
                {
                    MediaType mediaType = MediaType.GetByAlias("Film");
                    Media media = Media.MakeNew(txtTitle.Text, mediaType, new User(0), 1131);

                    //get the id of the umbracoFile property
                    int id = media.getProperty("filmFile").Id;
                    string destinationPath = Server.MapPath("~") + "media/" + id + "/";

                    if (!Directory.Exists(destinationPath))
                        Directory.CreateDirectory(destinationPath);

                    //copy over the file
                    File.Copy(sourceFile, destinationPath + fullClientFileName);

                    //add umbracoFile property
                    media.getProperty("filmFile").Value = "/media/" + id + "/" + fullClientFileName;

                    //add other properties
                    media.getProperty("umbracoBytes").Value = fileLength;
                    media.getProperty("umbracoExtension").Value = (Path.GetExtension(fullClientFileName).Length > 0 ? Path.GetExtension(fullClientFileName).Substring(1) : "");

                    media.Save();

                    return media;
                }
                catch (Exception ex)
                {
                    Log.Add(LogTypes.Error, -1, "Exception while creating new media item: " + ex.Message + ex.StackTrace);
                }
                return null;

     

    Hope that helps,
    Sascha

  • dcwoodsjr 12 posts 30 karma points
    Aug 11, 2010 @ 04:19
    dcwoodsjr
    0

    Thanks for the reply...Fortunately I didn't have to go through all that.

    My work around was to take the .pdf that I alre already had and used .net to write that directly to the file system. After that I wrote some code that built a link to the document and uploaded the link to my 'ServiceBulletin' upload property using documentservice.asmx. 

    That way our client still has the document upload data type that they require for manual upload, but the service that I've written can also download attachments from an email account that it monitors and upload the the link to the new document that has been written to file system.

    -dw

     

Please Sign in or register to post replies

Write your reply to:

Draft