Monday, 7 January 2013

Conditional String Concat

I was unable to find a way to allow conditional concat of strings, thus had to resort to the following

    <ItemGroup>
      <Docs Include="docs\*.*" /> 
    </ItemGroup>


    <WriteLinesToFile  File="DocumentNotInPdf.txt" 
     Condition="!$(LinesFromReadFile.Contains('%(Docs.FileName)'))" 
     Lines="%(Docs.FileName)%(Docs.Extension) "/>

    <ReadLinesFromFile File="DocumentNotInPdf.txt">
      <Output TaskParameter="Lines" PropertyName="DocNoPdf"/>
    </ReadLinesFromFile>
    
     <MSBuild.Community.Tasks.Mail Condition="Exists('DocumentNotInPdf.txt')"
        SmtpServer="smtp"
        To=""
        From=""
        Subject="CheckDocToPdf"
        Body="Files not in docToPdf121.bat - $(DocNoPdf)."
                                         
       />
 

Wednesday, 27 June 2012

Clear Team Site home.aspx by c# code

Here's how you clear the home.aspx page of a default Team Site programmatically
        public void clearContentOnMain()
        {
            try
            {
                using (SPSite site = new SPSite(TEAMSITEURL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFile homePage = web.GetFile("SitePages/Home.aspx");
                        if (homePage.Exists)
                        {
                            homePage.Item[SPBuiltInFieldId.WikiField] = "";
                            homePage.Item.Update();

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SPException("Error getting SitePages/Home.aspx " + ex.ToString());
            }

        }

Tuesday, 26 June 2012

User vs UserMulti

I spent almost a whole day trying to figure out why ViewFieldsOverride only imports the UserName of AssignedTo field in Content Query Web Part.

After trying various ways and banging my head against the table, it turns out that by changing Type="UserMulti" from Type="User", AssignedTo field will have the format 'ID;#UserName'.

Friday, 8 June 2012

Add Presence Icon to SPGridview

I spent hours trying to figure out why my presence image only appears for the last instance of the particular user, and I finally figured out why, id has to be unique.
to show image presence, I attached to rowdatabound event with the required javascript

       protected void gvProjOverview_RowDataBound(object sender, GridViewRowEventArgs e)
        { 
            if(e.Row.DataItem!=null)
            {
                SPWeb web = SPContext.Current.Web; 

                SPUser user = web.EnsureUser("DOMAIN\login");
 
                e.Row.Cells[4].Text = ""
                    + " "
                    + ""
                    + "" + user.Name +  ""
                    + "";
            }
        }

id='imn_" + e.Row.RowIndex <- this was where I had used user.ID which was not unique and thus the presence icon was only showing for one of the available users.

The lack of distinct ID causes some presence icon to disappear, that is, if user Andy is on two of the rows, the presence icon only appears for the last row where Andy is in.

Thursday, 7 June 2012

EditorWebPart wierd behavior

I was trying to implement EditorPart and I find that the webpart doesn't update when OK or Apply is clicked. Only when it's been refreshed then the changes appear.
Going through the code shows that the cycle order is : CreateChildControls ApplyChanges OnPreRender
So since I am setting the value in CreateChildControls, it was using the old value before the changes happen in ApplyChanges.
Moving code under CreateChildControls to be in OnPreRender sets automatic update when Apply/OK is clicked.

Wednesday, 6 June 2012

Extending the Content Query Web Part (ContentByQueryWebPart)

I was assigned a task to allow filtering of CQWP results without requiring the user to edit the webpart.
I figured out the following simple way to extend the CQWP by having the filtering done with a drop down list on the webpart itself.
At first I wanted to use the consumer/provider method but decided this was easier...
Here's the code:
namespace ExtendCQWP.FilterWP
{
    [ToolboxItemAttribute(false)]
    public class FilterWP: Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart
    {
        protected override void OnLoad(EventArgs e)
        { 
            if (this.Page.Request.QueryString["FilterField1"]!=null && this.Page.Request.QueryString["FilterValue1"]!=null)
            {
                this.FilterField1 = this.Page.Request.QueryString["FilterField1"].ToString(); 
                this.FilterValue1 = this.Page.Request.QueryString["FilterValue1"].ToString();
            }
        }
        protected override void CreateChildControls()
        {             
            DropDownList ddl = new DropDownList();
            ddl.Items.Add(new ListItem("All", "All"));            
            ddl.Items.Add(new ListItem("HR", "HR"));
            ddl.Items.Add(new ListItem("Finance", "Finance"));
            ddl.Items.Add(new ListItem("IT", "IT"));

            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            ddl.AutoPostBack = true;
            ddl.EnableViewState = true;

            if (this.Page.Request.QueryString["FilterValue1"] != null)
            { 
                foreach(ListItem item in ddl.Items)
                    if(item.Text.Equals(this.Page.Request.QueryString["FilterValue1"].ToString()))
                    {
                        item.Selected = true;
                        break;
                    }
            }

            this.Controls.Add(ddl);            
            base.CreateChildControls();
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList tmpddl = (DropDownList)sender;
            if(tmpddl.SelectedValue.Equals("All"))
                SPUtility.Redirect("http://mysite/SitePages/cq.aspx", SPRedirectFlags.Trusted, HttpContext.Current);
            else
                SPUtility.Redirect("http://mysite/SitePages/cq.aspx?FilterField1=Division&FilterValue1=" + tmpddl.SelectedValue, SPRedirectFlags.Trusted, HttpContext.Current);
        }
    }
}

Here's how the list and web part looks like.

Monday, 4 June 2012

Attempted to use an object that has ceased to exist

I received this error when trying to insert/edit web part
Attempted to use an object that has ceased to exist

The resolution was to remove USING statement when using SPContext.Current.Web;

This issue happened because SPContext.Current.Web disposes the web object automatically, and by adding a

using
statement, it will try to dispose it again when there's nothing to dispose.

SPMenuField disappears when/after Edit Mode

There has been an issue that has been bugging me when I create an SPMenuField, editing the page will result in the menu field columns disappearing.

After spending hours trialing and erroring, it was resolved by adding a .DataBind() in the Page_Load method.

UserControl.ascx.cs

        protected void Page_Load(object sender, EventArgs e)
        { 
          gvPPL.DataBind();
        }

UserControl.ascx


<SharePoint:MenuTemplate ID="gvPPLMenu" runat="server">
    <SharePoint:MenuItemTemplate ID="editMenu" runat="server" Text="Edit" ClientOnClickNavigateUrl="/_layouts/BPOSproperties/ProjectSettings.aspx?type=edit&url=%EDIT%" />
</SharePoint:MenuTemplate>
<SharePoint:SPGridView ID="gvPPL" runat="server" AutoGenerateColumns="false" DataSourceID="pplDataSource">
    <Columns>
        <SharePoint:SPMenuField HeaderText="NameMenu" TextFields="Name" MenuTemplateId="gvPPLMenu"
        NavigateUrlFields="Email" NavigateUrlFormat="{0}" TokenNameAndValueFields="EDIT=Email" />
        <SharePoint:SPBoundField HeaderText="Name" DataField="Name"  />
    </Columns>
</SharePoint:SPGridView>

<asp:ObjectDataSource ID="pplDataSource" runat="server" TypeName="BPOSsite.PeopleManager, BPOSsite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9ab07f514f0925b" SelectMethod="GetPeople"  />

People.cs


namespace BPOSsite
{
    public class People
    {
        private string name;
        private string email;

        public People(string n, string e)
        {
            this.email = e;
            this.name = n;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
    }

    public class PeopleManager
    {
        public List<People> GetPeople()
        {
            List<People> list = new List<People>();
            list.Add(new People("John", "john@mail.com"));
            list.Add(new People("Nick", "nick@mail.com"));
            list.Add(new People("Tracy", "tracy@mail.com"));
            list.Add(new People("Dick", "dick@mail.com"));
            return list; 
        }    
    }
}

Friday, 1 June 2012

ObjectDataSource TypeName not found

In sharepoint i had to list ObjectDataSource this way

<asp:ObjectDataSource ID="pplDataSource" runat="server" TypeName="MyNameSpace.PeopleManager, MyNameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9ab07f514f0925b" SelectMethod="GetPeople"  />

else I will get 'TypeName not found' error.

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: