jueves, 11 de julio de 2013

Usando la libreria DocX en SharePoint Parte III - Usando Custom properties en Word (DocX Library in SharePoint 2010 Part III - Custom Properties)

In this chapter we learn, how to use custom properties in a Word Document

In the latest chapters we have reviewed the Docx library, in this chapter we will continue, inserting custom properties in the Word document.





1. Open Word and create new document, see the next image.




2. In the tab "file", go to properties and select custom properties.



3. In the tab "custom", type: "MyName" and  the tab value type: "MyName", then press Add



4. Now, add our custom property.


5. Once you have everything ready, save the word document as "CustomPropertyTemplate.docx" and close Word.

6. Now go to sharePoint library and select open with explorer, see the next image.


7. when you open the folder, appears a new hidden folder called Forms, open this folder and paste the word document..






8. go to visual studio solution and create a new visual web part..
9. In the design view add a texbox and a button, see the next image.


10. then you add the next code in the clic event of the button.


11. All code  has already been reviewed previously, the most important it's the following code line.

                Doc.AddCustomProperty(new CustomProperty("MyName", TextBox1.Text));
                Doc.Save();

With this property, you can storing the textbox value.

12. execute and test.




And so we can handle custom properties.

Code.

 using System.Web.UI.WebControls.WebParts;  
 using Novacode;  
 using Microsoft.SharePoint;  
 using System.IO;  
 using System.Text;  
 namespace ProyectosSharePoint.CustomProperties  
 {  
   public partial class CustomPropertiesUserControl : UserControl  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       SPWeb Web = SPContext.Current.Web;  
       SPFile FileWord = null;  
       FileWord = Web.GetFile(Web.Url + "/DocxDocumentos/Forms/CustomPropertyTemplate is.docx");  
       Stream readStreamNovaCode = FileWord.OpenBinaryStream();  
       using (DocX Doc = DocX.Load(readStreamNovaCode))  
       {  
         Doc.AddCustomProperty(new CustomProperty("MyName", TextBox1.Text));  
         Doc.Save();  
         SPList olist = Web.Lists["DocxDocumentos"];  
         String url = olist.RootFolder.ServerRelativeUrl.ToString();  
         SPFolder newfolder = Web.GetFolder(url + "/");  
         SPFile uploadedFileDocNova = newfolder.Files.Add(newfolder.Url + @"/" + TextBox1.Text + ".docx", readStreamNovaCode, true);  
       }  
     }  
   }  
 }  

Regards.
Oscar Miguel Dominguez Acevedo.

No hay comentarios:

Publicar un comentario