31.10.2014 Views

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tutorials | 67<br />

Then we remove it:<br />

DocumentArchiveHandler.RemoveDocumentArchive( da );<br />

2.17.3 Add a Document<br />

To add a document to a DocumentArchive we need a document. The document is constructed with a<br />

DocumentArchive, a User and a file Stream.<br />

Import Necessary Namespaces<br />

First import the necessary namespaces that will be used to manage document archives and users. The namespace<br />

StarCommunity.Modules.DocumentArchive, StarCommunity.Core and StarCommunity.Core.Modules.Security is<br />

described by clicking on their names. We also need to import System.IO to use the Stream class. Make sure you add<br />

the assemblies as a reference, mentioned in Setting up Visual Studio.<br />

using System.IO;<br />

using StarCommunity.Core;<br />

using StarCommunity.Core.Modules.Security;<br />

using StarCommunity.Modules.DocumentArchive;<br />

using StarCommunity.Modules.MyPage;<br />

First we get the document archive in which we want to add our document. In this case it is the document archive that<br />

has been automatically created for us upon MyPage creation.<br />

//Get the user by id<br />

IUser user =<br />

(IUser)StarCommunitySystem.CurrentContext.DefaultSecurity.GetUser(1234);<br />

//Get the document archive for user via my page<br />

MyPage myPage = MyPageHandler.GetMyPage( user );<br />

DocumentArchive da = myPage.DocumentArchive;<br />

Now we can create the document by providing the user that is uploading the file the document archive and a file<br />

stream. In this case the archive owner is the same user as the uploader.<br />

//Get the file stream<br />

FileStream stream = new FileStream("foo.doc", FileMode.Open);<br />

//Create the document<br />

Document doc =<br />

new Document("foo.doc", "foo description", da, user, stream);<br />

Now we add the Document doc using the DocumentArchiveHandler and at the same time committ it to database,<br />

until now it has only been represented in memory.<br />

doc = DocumentArchiveHandler.AddDocument( doc );<br />

doc now contains the committed object with the unique ID property set.<br />

© <strong>EPiServer</strong> AB

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!