Subscribe: Caught in the .Net
Added By: Feedage Forager Feedage Grade B rated
Language: English
ajax  code  control  data  menu  microsoft  myrun  net framework  net  new  page  run  sharepoint  visual studio  work 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Caught in the .Net

Caught in the .Net

Tips, tricks, and knowledge sharing about Microsoft .NET technologies, including .NET Framework 2.0, 3.5, AJAX, ASP.NET, C#, Visual Studio 2008, and Sharepoint (WSS | MOSS)

Updated: 2016-09-08T00:37:42.968-04:00


Blog Moved...


... to

Please update your bookmarks.

How to get around OpenXML error: Can not replace the OpenXmlElement "newChild" because it is part of a tree


I was recently working on a MS Word doc generation project and kept getting this error.

It took me a while to figure out the source of the problem, but it made total sense...
When you are a building a document tree (example below), you are creating new elements like

var myRun = new Run(new Text("reusable text"));

var myParagraph = new Paragraph();


The 3rd line above ties in that Run object into the Paragraph object, so if you try to reuse the myRun object by appending it somewhere else - you will get this error.

I also tried creating a new Run() object using the old one as the object initializer:

var myRun2 = new Run(myRun);

That will throw the same error.
Why? Because the above line copies instantiates myRun2 with the reference of the myRun object so when you try to Append it elsewhere, you will get the error because it's technically the same object.

There is a very handy property that most OpenXML elements have - OuterXml. The other handy thing about it - it's in most class constructors so it can be used to instantiate an object.
In lamens terms, instead of

var myRun2 = new Run(myRun);


var myRun2 = new Run(myRun.OuterXml);

Sample tree:
  • SdtBlock
  • - Paragraph
  • ----Run
  • ------Text
  • ----Run
  • ------Break
  • ----Run
  • ------Text

10 Best Practices For Building SharePoint Solutions


Came across an excellent article in MSDN Magazine:

10 Best Practices For Building SharePoint Solutions

It's a must read if you are looking to deploy custom Sharepoint applications into your farm.



I have added Sharepoint as an additional topic that I will blog about.

I have been doing a lot more Sharepoint development and integration at work, and it's actually sparking an interest.

So, stay tuned.

OpenXML & Word Doc Manipulation using Sharepoint Data


Call me a lousy blogger but there is no code in this post because I've been busy refactoring it. That's not the only reason. I have been involved in a lot more Sharepoint development at work lately - moreso than .Net, so I've been kind of busy reading up SDKs and playing with APIs.On that note, I got a chance to get my hands dirty with some OpenXML 2.0. The cool thing about that was that it was @ Microsoft. Not only did I get to play with it, but met the guys that developed parts of it and even wrote 2,000 of the 6,000 pages of documentation - Tristan Davis and Zeyad Rajabi - who helped me a great deal.The story that I worked on involved gathering data from Sharepoint lists (some of it fed from the BDC(business data catalog)) and using OpenXML to populate a Word template. The real world scenario behind it is at my company, like I'm sure at a lot of other companies, we have administrative assistants who end up generating the documents for meetings. They just sit there copying and pasting data from various data sources and reports into this document. And there are as many documents generated as there are assistants multiplied by weeks in the year. Seriously - these people end up working 60 hour workweeks.The program utilizing OpenXML to generate these documents for them is actually pretty simple. I had some issues deploying it as a web app onto Sharepoint because of various little issues that arose along the way (running sharepoint on a 64-bit windows 2008 server w/ IIS 7 - some assemblies were 32-bit, so couldn't get it to run; other times, there were IIS7 issues. Note to self: improve knowledge of IIS7). Because of that, I wrote it as a WinForms app.The interface is pretty boring - it has 2 listboxes.The first listbox contains key data - in this case, a list of product names. This field is used as a foreign key to relate it to a bunch of other lists - sales data list, product manager list, and even a Sharepoint picture library.The second listbox contains a list of template documents from a document library that was created.There is also a textbox for indicating what the new filename is going to be once the document is generated, and a button that generates the document.So how does this all work?First of all, none of this would be possible if Microsoft hadn't created the new .docx, .xlsx, .pptx formats in Office 2007. As much as I cursed it for doing so - I have seen the light last week. The new format is purely XML based. Hence, OpenXML allows us to manipulate the hell out of our documents without ever opening up Microsoft Word.The one caveat is that Microsoft Word's Markup Language (WordML) is a bit cumbersome to learn right off the bat. The good news is that with a bit of blog reading from Brian Jones on OpenXML and an extremely powerful tool that comes with the SDK called DocumentReflector, you will be generating documents in no time. Here is a video by Alistair Spears that gives a demo of this wonderful tool: []So, how did I get data in from sharepoint lists into a word document? Let's take a look at the process step-by-step.Traversing Sharepoint Lists/Doc Libraries/Picture Libraries1. The first thing you will need is to connect to a sharepoint list. The code is the easy part - it's just 2 lines://declarationsprotected SPListItemCollection product_owner_items;protected SPListItemCollection template_doc_items;protected SPListItemCollection marketing_info_items;protected SPPictureLibrary product_model_pics;protected SPSite mysite;protected SPWeb myweb;mysite = new SPSite("http://izmqdp6452022/Docs/Lists/Product%20Owners/AllItems.aspx");myweb = mysite.AllWebs["Docs"];SPSite's constructor can actually determine what site you are connecting to in your Sharepoint farm based on the URL passed in. You just have to make sure that whichever user your a[...]

Back from the Dead


For a blogger, I've bit the dust in terms of frequent posts. However, I would rather post useful stuff once a month (or two) than post anything for the sake of posting.

Having said that, I know I haven't posted in a while. I've been occupied with two major projects.
  1. Nothin But .NET Bootcamp
In October 2008, I was given the opportunity to go to J.P Boodhoo's Nothin But .NET bootcamp ( It was a truly humbling experience and gave me a chance to set some goals for myself, in terms of self improvement in the .NET field.

At the end of December 2008, I launched an online luxury magazine for the publishers of Beverly Hills Report called WhatIsLuxury? (

It was a fun experience and in the coming weeks, I would like to share some of the tricks & techniques I've used to accomplish some challenges that their requirements presented, including:
  • AJAX-enabled story rotation on the home page
  • Full Page Ad rotation every X number of pages
  • AJAX-enabled thumbnail-based navigation
Thanks for reading and stay tuned!

Dynamic Drop-Down Menus in ASP.Net 2.0 / 3.5


One of the most essential components to a solid site is a versatile and intuitive navigational drop-down menu. There are many ways to do this. My goals, as always, when approaching a challenge:Let's not reinvent the wheel.There are many solutions out there. A big point of the .NET Framework is to provide reusable components so that you don't have to code things from scratch. Chances are it's already been developed and developed well.Avoid hard-coding where possible.It's all HTML, CSS, and Javascript by the time the menu renders in your browser, but that doesn't mean you need to code it in that way. We're trying to build something dynamic and avoid duplicating code from page to page. That means there is going to be a data feed, whether from XML or a database, and a control that can process and display this feed from page to page.Create an elegant, cross-browser compliant solution.Some of the out-of-the-box ASP.Net menu controls are not that pretty and don't really work across all browser platforms, especially Safari (Webkit based... speaking of which - guess what Google's Chrome uses ;-) ) We need something that will be readable, easy to maintain, and most importantly, work across all browsers.I found a couple of methods out there to accomplish this. Peter Bromberg talks about one way in his blog here ('s a variation of an ASP.Net 1.1 solution previously published in 2003 by WROX. It works, but it's a slight modification from the 1.1 version, and it seemed a little cumbersome to customize the look and feel, so I continued to look for something designed specifically for 2.0 and up that would be simple to use out-of-the-box and easy to customize.Surely enough, I stumbled upon a solution that met my three goals, written by ScottGu ( build a drop-down menu, we need the following:Data FeedCSS StylesheetServer-side control that will generate a menu from #1 and #2.The data feed is your standard web.sitemap ( that you can build in Visual Studio that looks something like this:The server side control is our typical with a SiteMapProvider, which is just responsible for reading in the data feed - in this case our web.sitemap file.The CSS is the tricky part. Using out-of-the-box simplified customizations won't get you your desired result, and if it will, chances are it probably won't look the same from browser to browser. Our solution:CSS Control Adapter Toolkit for ASP.Net 2.0You can download it here: toolkit essentially takes the worry out of coding for cross-browser compliance by providing ASP.Net controls that are already formatted in CSS to look pretty and the same in all common browsers, which is fantastic, because usually, you have to buy a component like this.For our drop-down menu, you can do vertical or horizontal; it uses CSS best practices by utilizing ul/li's instead of tables.Check out ScottGu's blog to get the code, documentation, and a more detailed explanation of exactly how to build, install, and use the drop-down menu with the CSS Control Adapter Toolkit.As always, I welcome your comments.UPDATE - 9/22/08As it turns out, the vertical menu doesn't display the sub-items properly in IE6. That doesn't work for me as more than 45% of my user base is on IE6. Stay tuned and I will post a fix for it.Applies 2.0 3.5 microsoft visual studio 2005 visual studio 2008 css xml menu drop-down navigation control adapter toolkit [...]

Making use of the 'Drop" in Drag-and-Drop in ASP.NET/AJAX


I find it a lot when developers get asked what is AJAX, a very common answer is - it's that technology that let's you drag and drop things without leaving the page.

Working within the .NET 2.0 or the 3.5 framework, there is functionality in the AJAX toolkit that let's you drag things using the DragPanel control in the AJAX control toolkit ( Very cool stuff if all you are trying to do is allow the user to reposition things. If you are trying to extend it for a more practical use, like being able to drag an item to a shopping cart, you don't really get that functionality out of the box with the AJAX control toolkit.

Looking at our objective from a 64,000 ft. view, we have this "hot" zone somewhere on a page. We have a couple of objects that can be dragged onto this zone, and when that happens, we want to be notified exactly what was dragged and to which zone, so that we can fire the appropriate code, such as add the object to our shopping cart.

How do we accomplish this given that this functionality is non-existent in the AJAX control toolkit? I was asking myself this question until I ran across a very cool solution by Jeff Prosise who writes for a section in the MSDN Magazine called Wicked Code. You can subscribe to it here:

The caveat with his solution is that it uses the Microsoft.Web.Preview library which comes as part of ASP.Net Futures (downloadable from here - The official description of the ASP.Net Futures is "Contains features currently being considered for future versions of ASP.NET and the .NET Framework," meaning you probably won't find too much documentation or support for it... or a guarantee of any sort that it will be included in the next release of the .NET Framework.

There is the fine print. If you are planning on deploying this to a high volume enterprise environment, it's certainly something to think about, however, Jeff's solution looks elegant, robust, and customizable to fit your need.

The code, article, and documentation is available here:

Great work Jeff!
Applies to:

Windows 2008 Server - Vista Alternative for your Desktop


This is a little bit off topic, but needed to be written...

If you like the aesthetics of Windows Vista, and, unless you are short on CPU, memory, or video resources, there is no reason not to, you have had debates with yourself. The almost-too-common debate of whether you should downgrade to XP or stay with Vista and deal with the errors, bugs, and problems... because it's just a pretty OS. So how do you get beauty AND brains without going to the Mac store?

Microsoft Windows 2008 Server
It's just like Vista, without the stupid bugs

Yes - that's the slogan they are going with.

I recommend this for any users running Vista on their desktops. The reason I don't recommend it for laptops is you won't get the best battery utilization and you won't have the options of putting your OS to sleep or hibernation.

To get Windows 2008 Server to act like a workstation, it will take some configuration to make it more user-friendly, and less like a server. I was referred to a blog that gives step-by-step instructions on accomplishing this:

If you have 2 days to spare, I recommend you try this out. Unless you upgrade from 32-bit Vista to a 64-bit Windows 2008 Server, most of your Vista drivers should work without a problem.

Use Macrium Reflect (click for free download) to ghost your current image, in case you want to go back.

To download the trial evaluation, check out:

Applies to:
, ,

Workaround to AuthenticationService security issue over HTTP


It's very convenient to be able to log in on a nice & functional page without leaving to go to a special secure & encrypted login page, hoping to be brought back to the page that you were on with all your work saved. The AJAX Framework in NET 3.5 provides a web service to help us accomplish that: AuthenticationService ( you are already on an encrypted HTTPS page, this works great. However, if you are serving out that nice & functional page over HTTP, because frankly, you don't need images or text encrypted, and would rather not use up overhead just for the sake of a login form, you may have a problem.Specifying the line below in your web.config won't really do anything.Changing the protocol to HTTPS in your path won't help you either: Why? Because it so happens that this version of AJAX, included in .NET 3.5 does not support cross-domain proxy authentication.So, how do you solve the problem of making sure your user's credentials don't get traced through unencrypted HTTP headers while keeping the page served over HTTP and not redirecting him/her anywhere?MD5 encryption via JavascriptBefore the password gets posted to your ASP.Net form, you encrypt it with MD5. MD5 is a one-way encryption algorithm that cannot be decrypted. To use it, you:Encrypt the submitted password value.Encrypt the DB-stored password associated with the submitted email address.Compare the two values. If equal, you can deem the authentication successful.With no need to reinvent the wheel, I found a great script for encrypting strings into MD5 format here: work Paul!ConclusionWith the shortcomings of the AJAX toolkit inside of .NET 3.5 Framework, Visual Studio 2008 is still a great IDE and before you go switching to PHP, all it takes is some time, creativity, and improvisation.Is it safe to use client-side encryption?Yes. Our goal is to prevent the HTTP headers from being compromised or traced in midstream, before they get to the server they are going to. As long as you are on a browser that supports JavaScript, Paul's MD5 function should do the trick.I will post some code later. I welcome your feedback.Applies to: Visual Studio 2008, .NET 3.5, AJAX[...]