Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Upload a File View modes: 
User avatar
Member
Member
pwn.singh86-hotmail - 1/17/2012 4:16:07 AM
   
Upload a File
Hi All ,

I have to upload a file of custom type document in Kentico 6.0.

Please tell me if anyone know how to implement the functionality in code behind ?


Thanks,
Pawan Singh

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 1/17/2012 6:05:56 AM
   
RE:Upload a File
Hi,

how to create a new document of particular document type is described here:
private bool CreateDocument()
{
// Create an instance of the Tree provider first
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the parent node - the API Example folder
TreeNode parentNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

if (parentNode != null)
{
// Create a new instance of the Tree node
TreeNode newNode = TreeNode.New("CMS.MenuItem", tree);

// Set the document's properties
newNode.DocumentName = "My new document";
newNode.DocumentCulture = "en-us";

// Insert the document into the content tree
newNode.Insert(parentNode);

return true;
}

return false;
}

And how to insert an image here:
private bool InsertUnsortedAttachment()
{
// Create a new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the document
TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

// Path to the file to be inserted. This example uses an explicitly defined file path. However, you can use an object of the HttpPostedFile type (uploaded via an upload control).
string postedFile = Server.MapPath("Files/file.png");

if (node != null)
{
// Insert the attachment
return (DocumentHelper.AddUnsortedAttachment(node, Guid.NewGuid(), postedFile, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE) != null);
}

return false;
}

More API examples you can find on your Kentico installation:
http://<your_domain>/CMSAPIExamples/Default.aspx

Best regards,
Ivana Tomanickova