Wednesday, 23 May 2012

How to create a word document with c# code

  • First download and install Open XML SDK 2.0 for Microsoft Office
  • Open the installed program, mine was located at C:\Program Files (x86)\Open XML SDK\V2.0\tool\OpenXmlSdkTool.exe
  • With the tool, open the desired word document that you want to generate with visual studio, temp.docx for example.
  • Click on 'Reflect Code' on the top pane. Notice how there will be code generated on the right hand frame.
  • In Visual Studio, create a new console application project, name it for example HelloWord. Add a new empty class WordDoc.cs and copy the above generated code to this new class file. Rename namespace to HelloWord and class to WordDoc. Here's how it looks like
  • Add the following two references
    • C:\Program Files (x86)\Open XML SDK\V2.0\lib\DocumentFormat.OpenXml.dll
    • WindowsBase from .NET (path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll)
  • add these two lines to the Main function
                WordDoc wd = new WordDoc();
                wd.CreatePackage(@"C:\temp\mydoc.docx");
    
  • Once you run the console, you should see mydoc.docx created in C:\temp.

Monday, 21 May 2012

Sharepoint 2010 Visio Workflow Visualization Error

I was faced with the following error when I tried to enable workflow visualization on SharePoint 2010

System.OperationCanceledException: The server failed to process the request. ---> System.ServiceModel.ServerTooBusyException: The HTTP service located at http://r9k7y7d:32843/80eb32d02df348f595a9d27e97f7238c/VisioGraphicsService.svc is too busy.  ---> System.Net.WebException: The remote server returned an error: (503) Server Unavailable.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeEndService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Office.Visio.Server.GraphicsServer.IVisioGraphicsService.EndGetVectorDiagram(IAsyncResult asyncResult)
   at Microsoft.Office.Visio.Server.Administration.VisioGraphicsServiceApplicationProxy.GetVectorDiagram(VectorDiagramRequestContract vectorDiagramRequest)
   at Microsoft.Office.Visio.Server.ServiceWrapper.GetVectorDiagram(String visioFileUrl, Int32 pageNumber, Boolean disableRefresh, AddonDataSource[] dataSources)
   --- End of inner exception stack trace ---
   at Microsoft.Office.Visio.Server.ServiceWrapper.GetVectorDiagram(String visioFileUrl, Int32 pageNumber, Boolean disableRefresh, AddonDataSource[] dataSources)
   at Microsoft.Office.Visio.Server.GetDiagramAsyncTask.GetVectorDiagram()

After some tinkering and scratching my head, these are the steps I took which made it work!

  1. First follow the steps here: http://technet.microsoft.com/en-us/library/ee524058.aspx
  2. Create an apppool that uses LocalSystem.
  3. What I did at first was to use the wrong app pool for my service. When I changed the AppPool to the one with Identity: LocalSystem, it worked!

Tuesday, 15 May 2012

Auto field update for Infopath in SharePoint (part 2)

This time I have used some Jquery to decrease the lines of code,
<script src="http://code.jquery.com/jquery-latest.js">
</script>
<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("FillOrWait");

var maxTime = 1000;
var waitPeriod = 500;
var totalWait = 0; 

function FillOrWait()
{

   if( $('input[title=OccupationField]').length==0 && totalWait<maxTime)
   {
      totalWait = totalWait + waitPeriod;       
      window.setTimeout(FillOrWait,waitPeriod);
   }
   else
   {
       var valueField = getQuerystring("Occupation");
       $('input[title=OccupationField]').val(valueField);
   }
}

function getQuerystring(key, default_)
{
 if (default_==null) default_="";
 key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
 var qs = regex.exec(window.location.href);
 if(qs == null)
  return default_;
 else
  return qs[1];
}
 </script>

Auto field update for Infopath in SharePoint

Refering to http://hermansberghem.blogspot.com/2011/03/trick-use-query-string-values-to.html, I am able to set the title of an InfoPath form using the Advance tab. So what I did was using my previous post of javascript snippets to get the element via Input tag and Title to set the field values.

As what the reference link has indicated, my internet explorer loads javascript before the infopath form, thus the text field did not exist while the javascript was running, I have used a polling method to wait half a second interval till the element loads.


Using infopath designer, the title is set to OccupationField

This snippet is similiar from previous javascript post except for a few tweaks.

The url will be something like http://url/page.aspx?Occupation=Policeman



Monday, 14 May 2012

Infopath submit to list

Following from http://msdn.microsoft.com/en-us/library/cc162745(v=office.12).aspx

Project and EmailType list will be populated with entries from InfoPath.

So each list will require data connections of

  1. Template in xml format for retrieving data
  2. Retriving data from the SharePoint list
  3. Submit data type to the _vti_bin/lists.asmx service of the SharePoint site

Below I have for Project, AddProjectTemplate(1), TestProject(2), Web Service Submit Proj (3).
Similiarly for EmailSequence, AddEmailSequenceTemplate(1), TestEmailSequence(2), Web Service Submit(3)



FormCode.cs
using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using mshtml;

namespace MyEmailType
{
    public partial class FormCode
    {
        public void InternalStartup()
        {
            EventManager.XmlEvents["/my:myFields/my:AddEmailType"].Changed += new XmlChangedEventHandler(AddEmailType_Changed);
            ((ButtonEvent)EventManager.ControlEvents["btnSubmit"]).Clicked += new ClickedEventHandler(btnSubmit_Clicked);             
            ((ButtonEvent)EventManager.ControlEvents["btnCancel"]).Clicked += new ClickedEventHandler(btnCancel_Clicked);
        }

        public void AddEmailType_Changed(object sender, XmlEventArgs e)
        {
            try
            {
                // Get a reference to the form's XmlNamespaceManager object.
                XmlNamespaceManager ns = this.NamespaceManager;

                // Create an XPathNavigator object for the form's main data
                // source.
                XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

                // If the check box is set to true, add a new row so the user 
                // can add a new contact.
                if (e.Site.Value == "true")
                {
                    XPathNavigator xnTable = xnDoc.SelectSingleNode("/my:myFields/my:gpEmailTypes", ns);
                    xnTable.AppendChild("");
                }
                else
                {
                    // If the user clears the check box, remove the added row.
                    ClearEnteredValues(xnDoc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("AddEmailType_Changed error occurred: " + ex.Message);
                throw;
            }
        }
        private void ClearEnteredValues(XPathNavigator xnDoc)
        {
            try
            {
                // Get a reference to the form's XmlNamespaceManager object.
                XmlNamespaceManager ns = this.NamespaceManager;

                // Create an XPathNodeIterator object to get a count of the 
                // rows in the repeating table used to add new Contacts.
                XPathNodeIterator xi = xnDoc.Select(
                   "/my:myFields/my:gpEmailTypes/my:gpEmailType", ns);
                int rowCount = xi.Count;

                if (rowCount > 0)
                {
                    // Get the first and last rows (nodes) in the 
                    // repeating table.
                    XPathNavigator firstNode = xnDoc.SelectSingleNode(
                       "/my:myFields/my:gpEmailTypes/my:gpEmailType[1]", ns);
                    XPathNavigator lastNode = xnDoc.SelectSingleNode(
                       "/my:myFields/my:gpEmailTypes/my:gpEmailTypes[" +
                       rowCount + "]", ns);

                    // Delete the existing nodes using the DeleteRange method.
                    firstNode.DeleteRange(lastNode);
                }

                // Clear the check box. 
                xnDoc.SelectSingleNode(
                   "/my:myFields/my:AddEmailType", ns).SetValue("false");
            }

            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " +
                   ex.Message);
                throw;
            }
        }

        public void btnSubmit_Clicked(object sender, ClickedEventArgs e)
        {
            try
            {                 
                XmlNamespaceManager ns = this.NamespaceManager;
 
                DataSource dsEmailSequence = this.DataSources["TestEmailSequence"];
 
                XPathNavigator xnEmailSequence = dsEmailSequence.CreateNavigator();

                XPathNodeIterator xiEmailSequence = xnEmailSequence.Select("/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW", ns);
 
                DataSource dsCAML = this.DataSources["AddEmailSequenceTemplate"];
 
                XPathNavigator xnCAML = dsCAML.CreateNavigator();
 
                WebServiceConnection wsSubmit = (WebServiceConnection)this.DataConnections["Web Service Submit"];
 
                XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

                XPathNodeIterator xiNewEmailType = xnDoc.Select("/my:myFields/my:gpEmailTypes/my:gpEmailType", ns);
                string projectTitle = xnDoc.SelectSingleNode("//my:myFields/my:ProjectTitle", NamespaceManager).Value;
                if (xiNewEmailType.Count > 0)
                {
                    while (xiNewEmailType.MoveNext())
                    {
                        
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='Title']", ns).SetValue(projectTitle);
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='EmailType']", ns).SetValue(xiNewEmailType.Current.SelectSingleNode("my:EmailType",ns).Value);
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='SeqFrom']", ns).SetValue(xiNewEmailType.Current.SelectSingleNode("my:SeqFrom", ns).Value);
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='SeqTo']", ns).SetValue(xiNewEmailType.Current.SelectSingleNode("my:SeqTo", ns).Value);
                        
                        xnCAML.SelectSingleNode("/Batch/Method/@Cmd", ns).SetValue("New");
                        // Submit the new row.
                        if(xiNewEmailType.Current.SelectSingleNode("my:SeqTo", ns).Value.Length>0)
                            wsSubmit.Execute();
                    }
                }
                
                // submit form add to project's info Project list
                WebServiceConnection wsSubmitProject = (WebServiceConnection)this.DataConnections["Web Service Submit Proj"];
                DataSource dsCAMLproject = this.DataSources["AddProjectTemplate"];

                XPathNavigator xnCAMLproject = dsCAMLproject.CreateNavigator();

                xnCAMLproject.SelectSingleNode("/Batch/Method/Field[@Name='Title']", ns).SetValue(projectTitle);
                wsSubmitProject.Execute();
                this.Application.ActiveWindow.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public void btnCancel_Clicked(object sender, ClickedEventArgs e)
        {
            this.Application.ActiveWindow.Close();
        }
    }
}

AddEmailSequenceTemplate.xml

<Batch OnError="Continue">
    <Method ID="1" Cmd="New">
        <Field Name='Title'></Field>
        <Field Name="EmailType"></Field>
        <Field Name="SeqFrom"></Field>
        <Field Name="SeqTo"></Field> 
        <Field Name="ID"></Field>
    </Method>
</Batch>

AddProjectTemplate.xml is similiar to above except with only Title and ID

Friday, 4 May 2012

Open an Infopath form template with Modal Dialog


<b><a onclick="javascript:openDialog();return false;" href="#" target="_self">

<span style="font-size: 12pt">Test Link</span></a> </b>

<script type="text/javascript">

function openDialog() 
{
 var options = {
      url: 'FormServerTemplates/MYINFOPATHURL',
      title: "Test",
      dialogReturnValueCallback: function (dialogResult) 
      {
                        SP.UI.ModalDialog.RefreshPage(dialogResult);
            if(dialogResult==1){     
                         }
      },
      allowMaximize:false
 };
 SP.UI.ModalDialog.showModalDialog(options);
} </script>







Infopath tutorial: http://msdn.microsoft.com/en-us/library/cc162745(v=office.12).aspx
Deploy infopath: http://msdn.microsoft.com/en-us/library/ms772110(v=office.12).aspx

Thursday, 3 May 2012

WCF DeleteObject

Because Orders table has a constaint to Order_Details table, the latter has to be deleted also with related Order.
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var custorder = lstOrders.SelectedItem;

                System.Type type = custorder.GetType();

                int orderid = (int)type.GetProperty("OrderID").GetValue(custorder, null);
                 
                Order odemo = ctx.Orders.Where(o => o.OrderID == orderid).ToList()[0];
                var odt = ctx.Order_Details.Where(od => od.OrderID == orderid).ToList();

                foreach(Order_Detail od in odt)
                        ctx.DeleteObject(od);

                ctx.DeleteObject(odemo);
                ctx.SaveChanges();
                MessageBox.Show("Deleted!");
                cmbCustomers_SelectionChanged(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.ToString());
            }
        }

Basic Linqpad Example

Wednesday, 2 May 2012

An attempt to track an entity or complex type failed because the entity or complex type 'OrderEntryProject.NWservice.Customer' does not implement the INotifyPropertyChanged interface.

I got this error when working on a WCF application,
"An attempt to track an entity or complex type failed because the entity or complex type 'OrderEntryProject.NWservice.Customer' does not implement the INotifyPropertyChanged interface."
After editing the Reference.cs file for Customer class, based on http://msdn.microsoft.com/en-us/library/ms229614(v=vs.90).aspx,
I added the following code to implement INotifyPropertyChanged in the class Customer,

public partial class Customer :  INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

 ......}
And the error disappears.

CAML joins revisited

Using the following list

Example: