Thursday, August 25, 2011

Client Object Model




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientOM = Microsoft.SharePoint.Client;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;

namespace ListPrinter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ShowButton_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            ListsListBox.Items.Clear();
            using(ClientOM.ClientContext ctx=new ClientOM.ClientContext(UrlTextBox.Text))
            {
                ClientOM.Web site=ctx.Web;
                ctx.Load(site);
                ctx.Load(site.Lists);
                ctx.ExecuteQuery();
                foreach (ClientOM.List list in site.Lists)
                {
                    ListsListBox.Items.Add(list.Title);
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void PrintButton_Click(object sender, EventArgs e)
        {
            if (ListsListBox.SelectedIndex > -1)
            {
                using (ClientOM.ClientContext ctx = new ClientOM.ClientContext(UrlTextBox.Text))
                {
                    string listTitle = ListsListBox.SelectedItem.ToString();
                    ClientOM.Web site = ctx.Web;
                    ctx.Load(site, s => s.Lists.Where(t => t.Title == listTitle));
                    ctx.ExecuteQuery();

                    ClientOM.List list = site.Lists[0];
                    ctx.Load(list, l => l.Fields.Where(f => f.Hidden == false && (f.CanBeDeleted || f.InternalName == "Title")));
                    ctx.ExecuteQuery();

                    ClientOM.ListItemCollection listItems = list.GetItems(ClientOM.CamlQuery.CreateAllFoldersQuery());
                    ctx.Load(listItems);
                    ctx.ExecuteQuery();

                    using (WordprocessingDocument package =
                WordprocessingDocument.Create("c:\\test1\\" + ListsListBox.SelectedItem.ToString() + ".docx",  WordprocessingDocumentType.Document))
                    {
                        Body body = new Body();
                        Table table = new Table();

                        //Columns
                        TableRow colRow = new TableRow();
                        foreach (ClientOM.Field field in list.Fields)
                        {
                            TableCell colCell = new TableCell();
                            colCell.Append(new Paragraph(new Run(new Text(field.Title))));
                            colRow.Append(colCell);
                        }
                        table.Append(colRow);

                        //Rows
                        foreach (ClientOM.ListItem item in listItems)
                        {
                            TableRow dataRow = new TableRow();

                            foreach (ClientOM.Field field in list.Fields)
                            {
                                TableCell dataCell = new TableCell();
                                string dataVal = string.Empty;
                                try
                                {
                                    dataVal = item[field.InternalName].ToString();
                                }
                                catch { dataVal = "-"; }
                                dataCell.Append(new Paragraph(new Run(new Text(dataVal))));
                                dataRow.Append(dataCell);
                            }

                            table.Append(dataRow);
                        }

                        body.Append(table);                       
                        package.AddMainDocumentPart();
                        package.MainDocumentPart.Document = new Document(body);
                        package.MainDocumentPart.Document.Save();
                        package.Close();
                    }
                }
                MessageBox.Show(" Document Created! ");
            }
        }
    }
}

No comments:

Post a Comment

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   ...

Popular posts