Monday, 30 July 2012

Workaround: Calling a JavaScript Function from a HyperLinkField in a GridView or SPGridView

If you place a HyperLinkField in a GridView and link it to a JavaScript function, then it doesn't work properly. The problem is that the URL specified by the HyperLinkField'sNavigateUrl fails to render when you use a JavaScript reference (such as "Javascript:func();") rather than a standard URL.

As a workaround, we can use the RowDataBoundEvent to prepare a proper call to the JavaScript function. To make this work, while assigning the value we should give "Javascript." (with a period) instead of"Javascript:" (with a colon)

void grid_RowDataBound(object sender, GridViewRowEventArgs e)
//if it is not DataRow return.
if (e.Row.RowType != DataControlRowType.DataRow)
//Loop thru the cells changing the "." to ":" in hyperlink navigate URLs
for (int i = 0; i < e.Row.Cells.Count; i++)
TableCell td = e.Row.Cells[i];
if (td.Controls.Count > 0 && td.Controls[0] is HyperLink)
HyperLink hyperLink = td.Controls[0] as HyperLink;
string navigateUrl = hyperLink.NavigateUrl.ToLower();
hyperLink.NavigateUrl = hyperLink.NavigateUrl.Replace(
navigateUrl.IndexOf("javascript."), "javascript.".Length),

Wednesday, 4 July 2012

Get/Set Values for Managed MetaData Field

Get Terms from the Field:
TaxonomyField commodityGrpFld = (TaxonomyField)web.Fields[“FieldName”];
       // get the Term Store ID from the field
       Guid commodityGrptermStoreId = commodityGrpFld.SspId;
       // Open a taxonomysession and get the correct termstore
       TaxonomySession session = new TaxonomySession(oSite);
       TermStore termStore = session.TermStores[commodityGrptermStoreId];               
       #region get terms from commodity termset
           TermSet termSetCommodity = termStore.GetTermSet(commodityGrpFld.TermSetId);                   
           if (termSetCommodity != null)
               TermCollection CommodityTermColl = termSetCommodity.Terms;
               ArrayList commodityList = new ArrayList();
               foreach (Term commTerm in CommodityTermColl)
//binding to listbox
               lbCommodityGrp.DataSource = commodityList;
        catch (Exception)
        { }
Update the Item value for Managed Metadata field:
TaxonomyField taxoField_Commodity =spfile.Item.Fields[[“FieldName”] as TaxonomyField;
        TaxonomySession taxoSession = new TaxonomySession(oSite);
        TermStore store = taxoSession.TermStores[taxoField_Commodity.SspId];
        TermSet termSet = store.GetTermSet(taxoField_Commodity.TermSetId);
        if (taxoField_Commodity.AllowMultipleValues)
          TermCollection terms = termSet.GetAllTerms();
          List<string> taxonomyValueList = new List<string>();
          foreach (ListItem item in lbCommodityGrp.Items)
            if (item.Selected)
          TaxonomyFieldValueCollection fieldValues = new TaxonomyFieldValueCollection(taxoField_Commodity);
          foreach (Term term in terms)
             if (taxonomyValueList.Contains(term.Name))
                TaxonomyFieldValue fieldValue = new TaxonomyFieldValue(taxoField_Commodity);
                fieldValue.TermGuid = term.Id.ToString();
                fieldValue.Label = term.Name;
          taxoField_Commodity.SetFieldValue(spfile.Item, fieldValues);
Get the Item value for Managed Metadata field:
                We need to convert the item value to TaxonomyFieldValueCollection

Monday, 2 July 2012

Using SharePoint 2010 Content Organizer to Route Documents

This video walks through the process of uploading, saving, and routing documents based on content type metadata values by levereging Microsoft SharePoint Server 2010 Content Organizer feature.

Programmatically Create a ‘Send To’ Connection to Send Documents to The Records Center in SharePoint 2010

If you have multiple send to connections to create or prefer to automate the deployment tasks related to your SharePoint environment, the following bit of code will help you create them automatically using the SPOfficialFileHost collection of your current site’s web application:

using (SPSite site = new SPSite(http://sp/))
    SPWebApplication webapp = site.WebApplication;
    SPOfficialFileHost newhost = new SPOfficialFileHost(true);
    newhost.OfficialFileUrl = new Uri(site.Url+"/records/_vti_bin/officialfile.asmx");
    newhost.OfficialFileName = "Records Center";
    newhost.ShowOnSendToMenu = true;
    newhost.Action = SPOfficialFileAction.Move;

The options for the action are the following:
1.       You can move the document from one location to another
2.       You can create a copy of the document and send it to another location
3.       Or you can move the document but leave a link of its new location in its former location