Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm currently researching workflow configuration possibilities delivered by Sharepoint 2010 (I am meaning the Visual Studio development side). The kind of configuration I'm meaning is the one done by technical persons, in the moment of workflow building/development. Some possibilities I found:

  • Accessing web.config file of the target webapp
  • Putting configuration data in some target webapp list

But they're not satisfactory enough. Is it possible to put a custom config file inside the workflow solution, and then access it in the workflow's runtime? If not, what would be possible workarounds?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Actually, it depends on who is supposed to set up the workflow and what kind of parameter has to be set (functional versus technical scope of the parameter).

If an user can change the parameters (within the web app), you can :

  1. Create an association form (prefered way I believe)
  2. Store the parameters in a custom list (can lead to errors if Id's are messed up)
  3. Create a custom GUI to read/write parameters from/to the property bag or a custom list. This leads to quite a lot of work.

If only technical users are supposed to setup the worklow, you can:

  1. store the parameters in the property bag (my preffered solution)
  2. store the parameters in the web.config. I don't like this, because it implies manual update of web.config

Of course, you can mix all of this

[Edit]: SPWeb, SPWebApplication, SPFarm and SPListItem have a property Properties which holds a dictionary of key/value. You can easily access and changes it's content using code or PowerShell.

Here is the code of one of my class I use in production to retrieve property bag values, in a hierarchical manner (First look at SPListItem, if not found at SPWeb, if not found at Web app, and lastly at Farm):

/// <summary>
/// PropertyUtilities provides a convenient way to retrieve properties accros the
/// hierarchy of objects in SharePoint
/// </summary>
public static class PropertyUtilities
{
    /// <summary>
    /// Try to retrieve a property on <see cref="SPWeb"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="web"><see cref="SPWeb"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="includeParentPropertyStores">If <c>true</c> also search on parent objects (SPSite, SPWebApplication or SPFarm).</param>
    /// <param name="result">The resulting property</param>
    /// <returns>The value of the property, or a default value for the type T</returns>
    public static bool TryGetProperty<T>(SPWeb web, string propertyName, bool includeParentPropertyStores, out T result)
    {
        if (web.AllProperties.ContainsKey(propertyName))
        {
            result = (T)web.GetProperty(propertyName);
            return true;
        }
        else if (includeParentPropertyStores)
        {
            if (web.ID == web.Site.RootWeb.ID)
            {
                return TryGetProperty<T>(web.Site.WebApplication, propertyName, true, out result);
            }
            else
            {
                return TryGetProperty<T>(web.ParentWeb, propertyName, true, out result);
            }
        }
        else
        {
            result = default(T);
            return false;
        }
    }

    /// <summary>
    /// Try to retrieve a property on <see cref="SPSite"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="site"><see cref="SPSite"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="includeParentPropertyStores">If <c>true</c> also search on parent objects ( SPWebApplication or SPFarm).</param>
    /// <param name="result">The resulting property</param>
    /// <returns>The value of the property, or a default value for the type T</returns>
    /// <remarks>The SPSite object does not actually contains a property bag. So we
    /// are using its root <see cref="SPWeb"/>'s property bag</remarks>
    public static bool TryGetProperty<T>(SPSite site, string propertyName, bool includeParentPropertyStores, out T result)
    {
        return TryGetProperty<T>(site.RootWeb, propertyName, includeParentPropertyStores, out result);
    }

    /// <summary>
    /// Try to retrieve a property on <see cref="SPWebApplication"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="webApp"><see cref="SPWebApplication"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="includeParentPropertyStores">If <c>true</c> also search on parent objects (SPFarm).</param>
    /// <param name="result">The resulting property</param>
    /// <returns>The value of the property, or a default value for the type T</returns>
    public static bool TryGetProperty<T>(SPWebApplication webApp, string propertyName, bool includeParentPropertyStores, out T result)
    {
        if (webApp.Properties.Contains(propertyName))
        {
            result = (T)webApp.Properties[propertyName];
            return true;
        }
        else if (includeParentPropertyStores)
        {
            return TryGetProperty<T>(webApp.Farm, propertyName, out result);
        }
        else
        {
            result = default(T);
            return false;
        }
    }

    /// <summary>
    /// Try to retrieve a property on <see cref="SPFarm"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="farm"><see cref="SPFarm"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="result">The resulting property</param>
    /// <returns>The value of the property, or a default value for the type T</returns>
    public static bool TryGetProperty<T>(SPFarm farm, string propertyName, out T result)
    {
        if (farm.Properties.Contains(propertyName))
        {
            result = (T)farm.Properties[propertyName];
            return true;
        }
        else
        {
            result = default(T);
            return false;
        }
    }

    /// <summary>
    /// Retrieve a property on <see cref="SPWeb"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="web"><see cref="SPWeb"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="includeParentPropertyStores">If <c>true</c> also search on parent objects (SPSite, SPWebApplication or SPFarm).</param>
    /// <returns>The value of the property if the property is defined, or null</returns>
    public static T GetProperty<T>(SPWeb web, string propertyName, bool includeParentPropertyStores) where T : class
    {
        T result;
        if (TryGetProperty<T>(web, propertyName, includeParentPropertyStores, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// Retrieve a property on <see cref="SPSite"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="site"><see cref="SPSite"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="includeParentPropertyStores">If <c>true</c> also search on parent objects ( SPWebApplication or SPFarm).</param>
    /// <returns>The value of the property if the property is defined, or null</returns>
    /// <remarks>The SPSite object does not actually contains a property bag. So we
    /// are using its root <see cref="SPWeb"/>'s property bag</remarks>
    public static T GetProperty<T>(SPSite site, string propertyName, bool includeParentPropertyStores) where T : class
    {
        T result;
        if (TryGetProperty<T>(site, propertyName, includeParentPropertyStores, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// Retrieve a property on <see cref="SPWebApplication"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="webApp"><see cref="SPWebApplication"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <param name="includeParentPropertyStores">If <c>true</c> also search on parent objects (SPFarm).</param>
    /// <returns>The value of the property if the property is defined, or null</returns>
    public static T GetProperty<T>(SPWebApplication webApp, string propertyName, bool includeParentPropertyStores) where T : class
    {
        T result;
        if (TryGetProperty<T>(webApp, propertyName, includeParentPropertyStores, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// Retrieve a property on <see cref="SPFarm"/> object
    /// </summary>
    /// <typeparam name="T">Type of the property</typeparam>
    /// <param name="farm"><see cref="SPFarm"/> object where to retrieve the property</param>
    /// <param name="propertyName">Name of the property</param>
    /// <returns>The value of the property if the property is defined, or null</returns>
    public static T GetProperty<T>(SPFarm farm, string propertyName) where T : class
    {
        T result;
        if (TryGetProperty<T>(farm, propertyName, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }
}

A script to set up this value :

$app = Get-SPWebApplication https://somewebapp
$app.Properties["webapp_scoped_property"] = "foo"
$app.Properties["webapp_scoped_other_property"] = "bar"
$app.Update()
share|improve this answer
    
I've edited my question, I mean the second kind of configuration. Besides, could you elaborate a little bit more on: store the parameters in the property bag (my preffered solution) just by providing some link / search keyword so I could research it –  soliloquyy Jan 18 '13 at 13:51
    
Great answer, thank you! –  soliloquyy Jan 18 '13 at 14:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.