// Thank you to the following blog for the code to enable AJAX use of the TaxonomyWebTaggingControl // http://pholpar.wordpress.com/2010/03/03/ajax-enabling-the-taxonomywebtaggingcontrol/ private string GetReloadJavaScript(TaxonomyWebTaggingControl taxonomyControl) { String script = String.Empty; String containerId = SPEncode.ScriptEncode(taxonomyControl.Controls[1].ClientID); Type type_TaxonomyWebTaggingControl = typeof(TaxonomyWebTaggingControl); MethodInfo mi_getOnloadJavascript = type_TaxonomyWebTaggingControl.GetMethod("getOnloadJavascript", BindingFlags.NonPublic | BindingFlags.Instance); String fullScript = (String)mi_getOnloadJavascript.Invoke(taxonomyControl, null); int pos = fullScript.IndexOf(String.Format("function {0}_load()", containerId)); if (pos > -1) { string endRequestFunctionName = string.Format("{0}_EndRequest", containerId); StringBuilder builder = new StringBuilder(); builder.Append("var myPrm = Sys.WebForms.PageRequestManager.getInstance();"); builder.Append("myPrm.add_endRequest(").Append(endRequestFunctionName).Append(");"); builder.Append("function ").Append(endRequestFunctionName).Append("(sender, args)"); builder.Append("{"); // we get te first part of the script needed to initialization // we start from pos 1, because we don't need the leading '{' builder.Append(fullScript.Substring(1, pos - 1)); builder.Append("Microsoft.SharePoint.Taxonomy.ScriptForWebTaggingUI.onLoad('"); builder.Append(containerId); builder.Append("');"); builder.Append("}}"); script = builder.ToString(); } return(script); }
public void InitialiseTaxonomyControl(TaxonomyWebTaggingControl taggingControl, String title, bool isMultiple, bool allowFillIn, Control displayControlUsingAJAX) { taggingControl.SspId.Add(TermStore.Id); taggingControl.GroupId = Group.Id; taggingControl.TermSetList = TermSet.Id.ToString(); taggingControl.AllowFillIn = allowFillIn; taggingControl.IsMulti = isMultiple; taggingControl.AnchorId = Guid.Empty; taggingControl.FieldName = title; if (displayControlUsingAJAX != null) { String key = "TaxonomyWebTaggingAjaxIncludeOnce_" + taggingControl.ID; if (!displayControlUsingAJAX.Page.ClientScript.IsClientScriptBlockRegistered(displayControlUsingAJAX.GetType(), key)) { displayControlUsingAJAX.Page.ClientScript.RegisterClientScriptBlock(displayControlUsingAJAX.GetType(), key, GetReloadJavaScript(taggingControl), true); } } }
protected string GetMMValue(TaxonomyWebTaggingControl tc) { StringBuilder mm = new StringBuilder(); try { var values = new TaxonomyFieldValueCollection(string.Empty); values.PopulateFromLabelGuidPairs(tc.Text); foreach (TaxonomyFieldValue value in values) { if (!string.IsNullOrEmpty(mm.ToString())) mm.Append("; "); mm.Append(value.Label); } } catch (Exception ex) { Utility.HandleException(ex, Controls); } return mm.ToString(); }
private void AddFormControl(SPField field, SPListItem listItem, SPList list, SPWeb spWeb) { // our table row HtmlTableRow newRow = new HtmlTableRow(); HtmlTableCell lblCell = new HtmlTableCell(); HtmlTableCell controlCell = new HtmlTableCell(); // our form control types TaxonomyWebTaggingControl taxonomyControl = null; BaseFieldControl webControl = null; DateTimeControl dateTimeControl = null; using (SPSite site = list.ParentWeb.Site) { using (SPWeb web = site.OpenWeb(spWeb.ID)) { if (field.FieldRenderingControl != null && !skipField(field)) { //add our label to the table System.Web.UI.WebControls.Label newLabel = new System.Web.UI.WebControls.Label(); newLabel.Width = Unit.Pixel(150); newLabel.Text = field.Title; lblCell.Controls.Add(newLabel); newRow.Cells.Add(lblCell); try { switch (field.FieldRenderingControl.GetType().Name) { case ("DateTimeField"): dateTimeControl = new DateTimeControl(); dateTimeControl.DateOnly = true; dateTimeControl.ID = string.Format("ctrl_{0}", field.InternalName); break; case ("TaxonomyFieldControl"): TaxonomySession session = new TaxonomySession(field.ParentList.ParentWeb.Site); var store = session.TermStores[0]; taxonomyControl = new TaxonomyWebTaggingControl(); taxonomyControl.IsMulti = true; taxonomyControl.IsAddTerms = true; taxonomyControl.TermSetId.Add(session.TermStores[0].Id); taxonomyControl.ID = string.Format("ctrl_{0}", field.InternalName); taxonomyControl.FieldName = field.Title; taxonomyControl.FieldId = field.Id.ToString(); taxonomyControl.SSPList = ((TaxonomyField)field).SspId.ToString(); taxonomyControl.AnchorId = ((TaxonomyField)field).AnchorId; taxonomyControl.TermSetList = ((TaxonomyField)field).TermSetId.ToString(); break; default: webControl = field.FieldRenderingControl; webControl.ID = string.Format("ctrl_{0}", field.InternalName); webControl.ControlMode = SPControlMode.New; webControl.ListId = list.ID; webControl.FieldName = field.InternalName; SPContext Context = SPContext.GetContext(HttpContext.Current, list.Items.GetItemById(listItem.ID).ID, list.ID, web); webControl.RenderContext = Context; webControl.ItemContext = Context; break; } //add our new row with controls to our placeholder phDynamicFormControls.Controls.Add(newRow); // add the cell into our row newRow.Cells.Add(controlCell); // add the row to the table phDynamicFormControls.Controls.Add(newRow); if (webControl != null) { controlCell.Controls.Add(webControl); } else if (taxonomyControl != null) { controlCell.Controls.Add(taxonomyControl); } else if (dateTimeControl != null) { controlCell.Controls.Add(dateTimeControl); } } catch (Exception ex) { } } } } }
public void InitialiseTaxonomyControl(TaxonomyWebTaggingControl taggingControl, String title, bool isMultiple) { InitialiseTaxonomyControl(taggingControl, title, isMultiple, false, null); }
/// <summary> /// Generates the form containing all the fields /// </summary> private void LoadForm() { try { using (new SPMonitoredScope("SP2010 BatchEdit Get Fields (and default values)")) { string webId = Request["web"]; string listId = Request["source"]; using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(webId))) { SPList list = web.Lists[new Guid(listId)]; foreach (SPField field in list.Fields) { if (field.ShowInNewForm.GetValueOrDefault(true) && field.FieldRenderingControl != null && (field.ShowInEditForm ?? field.CanBeDisplayedInEditForm)) { string lblText = field.Title; if (field.Required) { lblText += " *"; } pnlFields.Controls.Add(new System.Web.UI.WebControls.Label { Text = lblText }); pnlFields.Controls.Add(new Literal { Text = "<br/>" }); try { if (!field.FieldRenderingControl.GetType().Equals(typeof(TaxonomyFieldControl))) { BaseFieldControl editControl = field.FieldRenderingControl; editControl.ID = "fld_" + field.Id.ToString().Replace("-", "_"); // fix for Lookup picker Trace.Write(field.Id.ToString()); editControl.ControlMode = SPControlMode.New; editControl.ListId = list.ID; editControl.FieldName = field.InternalName; editControl.RenderContext = SPContext.GetContext(HttpContext.Current, list.DefaultView.ID, list.ID, web); pnlFields.Controls.Add(editControl); } else { var session = new TaxonomySession(field.ParentList.ParentWeb.Site); var taxonomyControl = new TaxonomyWebTaggingControl { IsMulti = true, IsAddTerms = true, ID = "fld_" + field.Id, FieldName = field.Title, FieldId = field.Id.ToString() }; taxonomyControl.TermSetId.Add(session.TermStores[0].Id); taxonomyControl.SSPList = ((TaxonomyField)field).SspId.ToString(); taxonomyControl.AnchorId = ((TaxonomyField)field).AnchorId; taxonomyControl.TermSetList = ((TaxonomyField)field).TermSetId.ToString(); pnlFields.Controls.Add(taxonomyControl); } } catch (Exception ex) { SPCriticalTraceCounter.AddDataToScope(66, "SP2010 BatchEdit", 1, ex.Message + ": " + ex.StackTrace); } pnlFields.Controls.Add(new Literal { Text = "<br/><br/>" }); } } } } } catch (Exception ex) { SPCriticalTraceCounter.AddDataToScope(66, "SP2010 BatchEdit", 1, ex.Message + ": " + ex.StackTrace); } }
/// <summary> /// Button ClickEvent will cause a PostBack and will try to save all values to corresponding /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btnOk_Click(object sender, EventArgs e) { using (new SPMonitoredScope("SP2010 BatchEdit Process Field Values")) { try { _fieldValues = new List <KeyValuePair <Guid, string> >(); foreach (Control control in pnlFields.Controls) { if (control.GetType().Equals(typeof(DateTimeField))) { BaseFieldControl taxControl = (DateTimeField)control; if (taxControl.Value != null) { _fieldValues.Add(new KeyValuePair <Guid, string>(taxControl.Field.Id, taxControl.Value.ToString())); } } else { var taggingControl = control as TaxonomyWebTaggingControl; if (taggingControl != null) { TaxonomyWebTaggingControl taxControl = taggingControl; if (!string.IsNullOrEmpty(taxControl.Text)) { _fieldValues.Add(new KeyValuePair <Guid, string>(new Guid(taxControl.FieldId), taxControl.Text)); } } else if (control.GetType().IsSubclassOf(typeof(BaseFieldControl))) { BaseFieldControl fieldControl = (BaseFieldControl)control; if (!string.IsNullOrEmpty(fieldControl.Value as string)) { _fieldValues.Add(new KeyValuePair <Guid, string>(fieldControl.Field.Id, fieldControl.Value as string)); } } } } string webId = Request["web"]; string listId = Request["source"]; string items = Request["items"]; using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(webId))) { SPList sourceLibrary = web.Lists[new Guid(listId)]; string[] itemids = items.Trim('|').Split('|'); foreach (string itemid in itemids) { try { SPListItem item = sourceLibrary.GetItemById(int.Parse(itemid)); if (item.Folder != null) { TraverseListFolder(item.Folder); } else { UpdateListItem(item); } } catch (Exception ex) { _messages += ex.Message + "<br/>"; SPCriticalTraceCounter.AddDataToScope(67, "SP2010 BatchEdit", 1, ex.Message + ": " + ex.StackTrace); } } } } catch (Exception ex) { _messages += ex.Message + "<br/>"; SPCriticalTraceCounter.AddDataToScope(68, "SP2010 BatchEdit", 1, ex.Message + ": " + ex.StackTrace); } } if (string.IsNullOrEmpty(_messages)) { Page.Response.Clear(); Page.Response.Write(string.Format(CultureInfo.InvariantCulture, "<script type=\"text/javascript\">window.frameElement.commonModalDialogClose(1, 'Items updated');</script>")); Page.Response.End(); } else { // Log all messages again. (we could also use this SPCriticalTraceCounter.AddDataToScope(67, "SP2010 BatchEdit", 1, _messages); lblMessages.Text = "Unfortunately there was an error updating the items with the provided values, a system administrator can use the message below to check upon this error, please provide them with it. <br />" + _messages; } }