Programmatically Add and Remove Items from the Quick Launch Bar

January 23rd, 2008 by mosslover

 I ran into a problem last week, where I need to update multiple sites navigation really quick by deleting and adding items to the navigation on the quick launch bar.  I found this great entry from Todd Baginski: /tbaginski/archive/2007/12/26/how-to-programmatically-customize-site-navigation-in-wss-3-0-and-moss-2007.aspx.  I wanted to take this a step further by removing nodes from underneath a specific heading.  Then I wanted to re-add different nodes, so that I could pull up some javascript that would change the links based on certain phrases (i.e. http://reports, http://sql/[Specific Annual Report]).  Here is the sample code for the quick launch additions and removals:

 SiteName = "your site name here";

                    using (SPSite site = new SPSite(SiteName))
                    {

                        SPWeb quickLaunchWeb = site.OpenWeb();

                        SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;
                       
                        //deletes the first and second child underneath the 7th heading on the page
                        quickLaunchNodesDevil.Children.Delete(quickLaunchNodesDevil.Children[0]);
                        quickLaunchNodesDevil.Children.Delete(quickLaunchNodesDevil.Children[1]);

//Notice that deleting any children is easy as long as you know the position…You can probably also check the children's names too and create a dynamic component and loop through the quick launch list and delete items by adding an if or for loop.

                        //creates 5 children underneath the 7th heading
                        SPNavigationNodeCollection quickLaunchNodes2 = quickLaunchWeb.Navigation.QuickLaunch;

                        SPNavigationNode externalSubMenuItem1 = new SPNavigationNode("Link1", "Url1", true);

                        quickLaunchNodes2Devil.Children.AddAsLast(externalSubMenuItem1);

                        SPNavigationNode externalSubMenuItem2 = new SPNavigationNode("Link2", "Url2", true);

                        quickLaunchNodes2Devil.Children.AddAsLast(externalSubMenuItem2);

                        SPNavigationNode externalSubMenuItem3 = new SPNavigationNode("Link3", "Url3", true);

                       quickLaunchNodes2Devil.Children.AddAsLast(externalSubMenuItem3);

                        quickLaunchWeb.Navigation.UseShared = true;

                         //You only need one update statement for all the remove and adds, on first shot I accidentally added it twice and learned this lesson.
                         quickLaunchWeb.Update();
                    }

I came up with this program really quick and it was a console application.  I added the first part of the site and had a console.readline statement to type in the site number, because ours were numerically based.  I wanted to double check every site to see if anything was broken in other areas.  If you want a quicker version, then you could probably just run any site in that specific web or read from an outside data source.  For example,  we have a web service that lists all the sites I could have called the web service to get the number and add/remove items from the quick launch with one go at the program.  If you guys would like a script example on how to use these items that I added to the quick launch to change the links with one file let me know.  I have a script that dynamically replaces specific text through the master file.  I hope that you guys enjoy.

Silly Error I should have known better…

January 9th, 2008 by mosslover

Yesterday I messed with the default.master file for our main portal page to allow for more real estate on the site.  Today I noticed that the lists had a problem showing the bread crumbs on every single site, so I created another masterpage for only the lists and document libraries, unfortunately I did not realize what I had done…A bit later I got some complaints about "File Not Found" errors, so I realized that I did not check the file back in.  Later on people start complaining about messages that the list is deleted and I could see it without any problem.  I spent  hours on this problem, then I realized that I need to approve the default.master page.  Remember to check in and approve all default.master pages if you are using any type of publishing site or you will most likely fall into an Occam's Razor like I did.  If you have trouble remembering this problem just remember the movie Contact and the definition of "Occam's Razor", which is the simplest solution is usually the answer to your problem.  You should really go out and see Contact if you haven't.  I would say it's one of the better Science Fiction Films out there.  Have a nice night kids:)

How to get a mailto link in your SPGrid Webpart without using Javascript…

January 9th, 2008 by mosslover

So I was trying to add paging to webpart on the top, rather than the bottom.  The code that someone else had wrote in javascript broke, when you added the the two lines of code to make paging work on the top.  I hate javascript and wanted a prettier solution.  So I did a ton of research and played around with some options.  I finally settled on a TemplateField, however I had to do some playing around and digging on google, but here is what you need to do:

Add this code to CreateChildControls:
            TemplateField colName= new TemplateField();
            colName.HeaderText = "Primary NAM";
            colName.SortExpression = "Column Name from DataSource";
            colName.ItemTemplate = new LinkTemplate("Names Column Name", "E-mail Column Name");
            oGrid.Columns.Add(colName);
 

Put this below your webpart class:
public class LinkTemplate : ITemplate
    {
        string _fieldName;
        string _emailName;
        public LinkTemplate(string FieldName, string EmailName)
        {
            _fieldName = FieldName;
            _emailName = EmailName;
           
        }
        public void InstantiateIn(Control container)
        {
            HyperLink l = new HyperLink();
            container.Controls.Add(l);
            l.DataBinding += new EventHandler(l_DataBinding);
        }
        private void l_DataBinding(Object sender, EventArgs e)
        {
            HyperLink l = (HyperLink)sender;
            DataRowView drv = (DataRowView)((SPGridViewRow)l.NamingContainer).DataItem;
            l.Text = drv[_fieldName].ToString();
            l.NavigateUrl = "mailto:" + drv[_emailName].ToString();
        }
    }

There you go a link with a mailto address that fixes any HyperLinkField issues.  Enjoy everyone:)         

I'm Not Dead…

January 7th, 2008 by mosslover

Ever since I got back from Florida I have been incredibly busy at work or sick.  Since last Monday I have had something I cannot shake, so I went to an RN at a CVS Minute Clinic.  It turns out I have a really nasty sinus infection.  Ever since Friday I have been sleeping and watching vhs and dvd movies that I can get from either Blockbuster or my own personal collection.  Hopefully by the end of the week I will be completely rid of this illness, so until then you won't see much coming out of this blog.  I had about 10 blog topics I wanted to write about this week, but forgot them all due to my low attention span.  So I hope that you all had a good holidays and are having  good year so far.  I will catch you all later. 

Heading down to Florida…

December 18th, 2007 by mosslover

I'm heading down to Fort Lauderdale tomorrow to visit my grandmother and soak up the lovely weather.  I won't be back in KC until Christmas Day.  I hope that everyone has a happy holidays if they celebrate the upcoming ones.  Sorry for the lack of excellent posting lately, hopefully when I get back that will change.  And I'm out:)

Export to Spreadsheet XSLT View Issue

December 17th, 2007 by mosslover

I am working on a really bizarre issue that occurred right after I installed SP1 for WSS 3.0 and MOSS.  Basically, Export to Spreadsheet works in a regular document library or regular document library webpart, however when I use an XSLT view or create a custom menu item using Sharepoint:ActionsMenu I get an "Unexpected error has occurred.  Changes to your data cannot be saved."  Every other menu item in XSLT view or on the custom menu works properly.  If anyone can shed light on this issue please let me know.  I am going to continue to look around for a solution and if I find one I will post it. 

Filtering on SPGridview Help

December 13th, 2007 by mosslover

I am referencing this blog: /bobsbonanza/archive/2007/05/14/filtering-with-spgridview.aspx.  For some reason when I add in the data source object it does not work.  I get a huge error and it will not filter any columns.  I am editing a webpart that someone else wrote and it looks like they are using a dataset that populates a view that populates the grid.  I want to filter off of the grid, but I assuming the problem is that it won't accept the grid it will only accept a method.  Anyone who has a good suggestion on what to do I'm all ears. 

Free Vista, Trips, Office, or Money

December 12th, 2007 by mosslover

I saw something called The Windows Feedback Program on Slashdot today.  Apparently, if you agree to be monitored and shared feedback with Microsoft, then you can get a free copy of one of the titles mentioned in my heading.  Here is the link: http://wfp.microsoft.com/Welcome.aspx.  It sounds pretty easy, imho, and you save about $100+ on a program.  Hopefully, it stops some people from illegally downloading the programs over the net too:)

Happy Hannukah!

December 3rd, 2007 by mosslover

For all  of those Sharepoint Bloggers out there who celebrate Hannukah like myself I wish you a Chag Sameach!  I am in Asp .Net 2.0 training this week, so I won't be blogging at all.  Then the Installfest for VS 2008 is tomorrow night, which is going to kill the first night of lighting for me.  Here is a wiki on the festival of lights for those of you who are interested and have no idea what this Jewish Holiday means:http://en.wikipedia.org/wiki/Hannukah.  I bet there is also instructions for playing the dreidel game if you are interested.  Have a good week all.

Microsoft Free VPC What I learned Using it…

November 30th, 2007 by mosslover

I started installing VS 2005 in the VPC and then I got to the point where I wanted to join my work domain.  The network person came by and because it's a domain controller she told me it could not be joined without a painful demotion from domain controller process.  I did some research also and realized the VPC will die in October of 2008 no matter what type of keys you input into the VPC.  So yet again I started from scratch installing my companies software, rather than using the MS VPC.  I had a little bit of a falling out with my old VPC, since LRS owns most of the codes and when I switched to a laptop the copying process did not go over too well.  I have also realized that I need to work with more Differencing Disks, because if I make my VHD read only and work with a Differencing Disk all I need to do is re-create a new one if I break the VPC.  For some reason I am constantly breaking my Sharepoint VPC's for the strangest reasons.  There is that one problem I had where it just decided to no longer boot Sharepoint and did not shoot off any error messages.  Then there is the time I converted to Active Directory and broke the entire VPC without realizing what I was doing.  I am going to strongly urge you guys to work with a Differencing Disk and create your own VPCs if you have the software.  If you don't have the software, then I would use MS's VPC.  Unfortunately, if you don't have a Server 2003 Key from RTM 2, then you are going to have to continuously delete the VPC and reinstall it from the rar files.  Good luck.