Question

mi problem is this, i got an XML in the Medialibrary and i need to modify a few thing but the strear is read only, any ideas?

MY CODE

        string nodeId = string.Empty;
        List<XmlNode> nodes = GetAll(); // Get all the nodes in my XML

        foreach (XmlNode node in nodes)
        {
            nodeId = node.SelectSingleNode(".//id").InnerText;
            if (nodeId == id) // id the id of the node that i'm looking for
            {
                node.ParentNode.RemoveChild(node);
                node.OwnerDocument.Save(MediaManager.GetMedia(mitem).GetStream().Stream);   
                return;
            }
        }
Was it helpful?

Solution

You're just trying to save the document like you would a file on disk, you need to upload the modified stream back into the Media Library:

http://briancaos.wordpress.com/2009/07/09/adding-a-file-to-the-sitecore-media-library-programatically/

Remember, in essence you're uploading a new document, not modifying an existing one.

Here is some sample code to retrieve a XML document from the media library, add a new node and save it back to the same location.

string mediaItemPath = "/sitecore/media library/Files/TestDoc";
string fileName = "TestDoc.xml";

//get the content db, i.e. master db
Sitecore.Data.Database contentDB = Sitecore.Context.ContentDatabase ?? Sitecore.Context.Database;

//get the media file
MediaItem mi = contentDB.GetItem(mediaItemPath);

if (mi == null)
    throw new ItemNotFoundException(mediaItemPath);

if (!mi.Extension.Equals("xml") || !mi.MimeType.Equals("text/xml"))
    throw new MediaException(string.Format("File {0} was not of correct XML format", mediaItemPath));

//load xml document using media stream
var xDoc = System.Xml.Linq.XDocument.Load(mi.GetMediaStream());

////manipulate the xml as required
////-- update or add new node or whatever
string nodeName = "Item";
string nodeValue = string.Format("{0} {1}", "test node ", DateTime.Now.ToString("yyyyMMddhhmmss"));
xDoc.Element("rootNode")
    .Add(new System.Xml.Linq.XElement(nodeName, nodeValue));

//save the modified document into a stream
var xmlStream = new System.IO.MemoryStream();
xDoc.Save(xmlStream);

// Create the options
var options = new Sitecore.Resources.Media.MediaCreatorOptions
                    {
                        FileBased = false, // Store the file in the database, not as a file
                        IncludeExtensionInItemName = false, // Keep file extension in item name
                        KeepExisting = false, // Keep any existing file with the same name
                        Versioned = false, // Do not make a versioned template
                        Destination = mediaItemPath, // set the path
                        Database = contentDB // Set the database
                    };

//Use a security disabler to allow changes
using (new Sitecore.SecurityModel.SecurityDisabler())
{
    //save the item back into media library
    Item mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream(xmlStream, fileName, options);
    xmlStream.Dispose();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top