Un esempio di come settare le date in InfoPath 2007 (usato come form server in SharePoint 2007 - MOSS).

Per settare una data non è sufficiente usare il metodo SetValue dell'oggetto XPathNavigator, ci sono una serie di problematiche/bug:
  • la data va passata come stringa nel formato yyyy-MM-dd
  • prima di settare un valore bisogna rimuovere l'attributo xsi:nil (o impostarlo a false)
  • se la data va settata a blank (string vuota), bisogna ricostruire il tag xml facendo in modo che sia un unico tag chiuso ed aggiungere l'attributo xsi:nil.
    Ovvero
    <my:dataSgart xsi:nil="true" /> corretto
    <my:dataSgart xsi:nil="true"></my:dataSgart> ERRATO
  • se la data era obbligatoria (Cannot be blank) NON va impostato l'attributo xsi:nil anche se valorizzata a blank

C#

private void SetDate(string key, DateTime dtDate)
{
  SetDate(key, dtDate.ToString("yyyy-MM-dd"));
}

private void SetDate(string key, string vDate)
{
  System.Xml.XPath.XPathNavigator ds = this.MainDataSource.CreateNavigator();
  XPathNavigator xn = ds.SelectSingleNode(key, NamespaceManager);
  if (xn.MoveToAttribute("nil", this.NamespaceManager.LookupNamespace("xsi")))
    xn.DeleteSelf();
  xn.SetValue(vDate);
  if (string.IsNullOrEmpty(vDate) == true)
    try
   {
      InsertNil(xn);
    } catch { // solo per Cannot be blank 
    }
}

public static void InsertNil(XPathNavigator node)
{
  if (!node.MoveToAttribute("nil",  this.NamespaceManager.LookupNamespace("xsi")))
  {
    Match m = Regex.Match(node.OuterXml, @"^<.+?></.+?>");
    if (m.Success)
    {
      string result = string.Empty;
      result = Regex.Replace(node.OuterXml, @"</.+?>", "", RegexOptions.IgnoreCase); 
      result = Regex.Replace(result, @">", "/>", RegexOptions.IgnoreCase);
      node.OuterXml = result;
    }
    node.CreateAttribute("xsi", "nil",  this.NamespaceManager.LookupNamespace("xsi"), "true");
  }
} 
info prese da http://www.sharepointblogs.com/dooke...ect-way.aspx .
Tags:
InfoPath9 SharePoint497 SharePoint 2007218
Potrebbe interessarti anche: