Wednesday, December 28, 2011

Programmatically search a SharePoint list for a specific items

If you'd like to programmatically search a SharePoint list for specific list items that contain a certain value in one of the lists' fields, the following sample code will allow you to obtain any list items that precisely match the specified criteria:
    string searchValue = "ECM Project";
    using (SPSite site = new SPSite(siteGuid))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList projectList = web.Lists["Project List"];
            SPQuery query = new SPQuery();
            query.Query = string.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>", searchValue);
            SPListItemCollection listItems = projectList.GetItems(query);
            web.AllowUnsafeUpdates = true;
            foreach (SPListItem listItem in listItems)
            {
                listItem["Title"] = "Enterprise Content Management Project";
                listItem.Update();
            }
            web.AllowUnsafeUpdates = false;
        }
    }
In this example, I'm performing a search on a custom SharePoint list called "Project List" for any list items that have "ECM Project" for the Title field. Once I find those list items, I replace the Title field with a value of "Enterprise Content Managment Project".

How to temporarily disable the Shift+R shortcut that brings up the IE Toolbar Ruler window

If you're constantly being plagued by the IE Toolbar Ruler window popping up every time you hit Shift+R, I found that you can temporarily disable it by hitting Ctrl+R. Unfortunately, this is only a temporary solution; however, it works long enough to be an effective means for dealing with this nagging issue.

Thursday, December 22, 2011

Programmatically add a user-friendly name in place of a hyperlink in a SPListItem Link field

There may come a time in which you would like to specify concise, user-friendly text instead of the hyperlink itself within a hyperlink field/column contained in a SharePoint list.  To do this, you simply add a comma followed by a space within your hyperlink field between the URL and text you wish to display and SharePoint will display the text after the comma as opposed to the hyperlink (i.e. "http://www.google.com/, Click here to go to Google").  Naturally, if the user clicks on the link, he/she will be redirected to the url specified in your hyperlink field.  In this example, I'm inserting a new SPListItem into a custom SPList and updating the hyperlink field, called Link, to display "Sherman's Blog" as opposed to the url, http://shermanstechnicalblog.blogspot.com/.  Here is sample code that demonstrates this technique:

            string blogTitle = "Sherman's Blog";
            string blogUrl = "http://shermanstechnicalblog.blogspot.com/";
            using (SPSite rootSite = new SPSite(rootSiteUrl))
            {
                using (SPWeb rootWeb = rootSite.OpenWeb())
                {
                    SPList blogList = rootWeb.Lists["Blog Directory"];
                    SPListItem item = blogList.Items.Add();
                    item["Title"] = "My Blog";
                    item["Link"] = string.Format("{0}, {1}", blogTitle, blogUrl);
                    item.Update();
                }
            }

Wednesday, December 21, 2011

WSPBuilder configuration settings that will allow the created ddf and manifest.xml files to be retained

If you're using Carsten Keutmann'sWSPBuilder to build your deployment packages and wish to retain the ddf and manifest.xml files that it creates, you can do so by adding or, if they already exist, updating the respective <appSettings> nodes in the WSPBuilder.exe.config file as follows:

Tuesday, December 20, 2011

Replace the logo on your SharePoint site via a Feature

The following C# class is the code you can associate with a feature that will replace the standard logo of your SharePoint site with a custom logo once that feature is activated.  When the feature is deactivated, it will reset the logo back to the default SharePoint logo.  Naturally, you'll want to update the reference to the logo with the proper location in which your custom logo is stored under the 12 (SharePoint 2007) or 14 (SharePoint 2010) hive.  In this particalur instance, I've got mine located under "_layouts/IMAGES/CustomLogoFolder/Custom_logo.png" (which translates to 14/TEMPLATE/IMAGES/CustomLogoFolder/Custom_logo.png on a SharePoint 2010 farm).

    public class UpdateLogoFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            base.FeatureActivated(properties);
            SPWeb currentWeb;
            using (currentWeb = properties.Feature.Parent as SPWeb)
            {
                try
                {
                    currentWeb.AllowUnsafeUpdates = true;
                    StringBuilder sbServerRelativeUrl = new StringBuilder(currentWeb.ServerRelativeUrl);
                    if (!currentWeb.ServerRelativeUrl.EndsWith(@"/"))
                    {
                        sbServerRelativeUrl.Append(@"/");
                    }
                    StringBuilder sbSiteLogoUrl = new StringBuilder(sbServerRelativeUrl.ToString());
                    sbSiteLogoUrl.Append(@"_layouts/IMAGES/CustomLogoFolder/Custom_logo.png");
                    currentWeb.SiteLogoUrl = sbSiteLogoUrl.ToString();
                    currentWeb.Update();
                }
                catch (Exception ex)
                {
                    WriteToEventLog(ex.Message, EventLogEntryType.Error);
                }
                finally
                {
                    currentWeb.AllowUnsafeUpdates = false;
                }
            }
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            base.FeatureDeactivating(properties);
            SPWeb currentWeb;
            using (currentWeb = properties.Feature.Parent as SPWeb)
            {
                try
                {
                    currentWeb.AllowUnsafeUpdates = true;
                    StringBuilder sbServerRelativeUrl = new StringBuilder(currentWeb.ServerRelativeUrl);
                    if (!currentWeb.ServerRelativeUrl.EndsWith(@"/"))
                    {
                        sbServerRelativeUrl.Append(@"/");
                    }
                    StringBuilder sbSiteLogoUrl = new StringBuilder(sbServerRelativeUrl.ToString());
                    sbSiteLogoUrl.Append(@"_layouts/IMAGES/siteicon.png");
                    currentWeb.SiteLogoUrl = sbSiteLogoUrl.ToString();
                    currentWeb.Update();
                }
                catch (Exception ex)
                {
                    WriteToEventLog(ex.Message, EventLogEntryType.Error);
                }
                finally
                {
                    currentWeb.AllowUnsafeUpdates = false;
                }
            }
        }
    }

Monday, December 19, 2011

Deploying/retracting a SharePoint 2010 solution that installs a GAC-deployed feature via Powershell

This might be useful information if you're attempting to deploy a .wsp file that installs a GAC-deployed feature on a SharePoint 2010 farm:
Accessing Powershell:
  1. Click Start -> Microsoft SharePoint 2010 Products -> SharePoint 2010 Management Shell
Deploying the solution:
  1. Add-spsolution -literalpath \\filepath\wspfilename.wsp
  2. Install-spsolution -identity wspfilename.wsp -webapplication http://webapplicationname -gacdeploy
  3. Enable-spfeature featuredirectoryname -Url http://webapplicationname
Retracting the solution:

  1. Disable-spfeature featurefoldername -Url http://webapplicationname
  2. Uninstall-spsolution -identity wspfilename.wsp -webapplication http://webapplicationname
  3. Remove-spsolution -identity wspfilename.wsp

Remove all leading and lagging empty spaces from items in an array

I was looking for a way to efficiently remove all leading and lagging empty spaces from items in an array in as few lines of code as possible.  In this particular instance, the goal was to read items into an array from the web.config file and then use the Trim() method to remove all leading and lagging empty spaces from each item.  Here is the code that will do this activity in just two lines for a particular, comma-delimited AppSetting node called ChargeCodesToUse in my web.config file:

string[] chargeCodesToUse = ConfigurationManager.AppSettings["ChargeCodesToUse"].ToString().Split(',');
Array.ForEach(chargeCodesToUse, chargeCodes => chargeCodesToUse[Array.IndexOf(chargeCodesToUse, chargeCodes)] = chargeCodes.Trim());