Gabriele Del Giovine Blog (Z80 powered brain...)


Un blog su quel che faccio (o cerco di fare...)
Calendario
aprile 2024
lmmgvsd
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Archivio Posts
Anno 2011

Anno 2010

Anno 2009

Anno 2008

Anno 2007

Anno 2006

Anno 2005
Links
    Mappa
    Blogs Amici
      Statistiche
      • Views Home Page: 301.506
      • Views Posts: 366.063
      • Views Gallerie: 256
      • n° Posts: 163
      • n° Commenti: 198

      SharePoint Dev Therapy: pillola UpdateLookupReferences

      SharePoint Dev Therapy: una pillolina al giorno leva il bug di torno...
      Lo scopo di questo che dovrebbe essere un appuntamento quasi quotidiano è quello di fornire agli sviluppatori SharePoint, sopratutto quelli alle prime armi una serie di funzioni di uso generale da usare nel proprio lavoro quotidiano. Cosi come una mela al giorno dovrebbe levare il medico di torno, queste pilloline quotidiane dovrebbero togliere i bug di torno...

      Oggi tocca alla pillolina che implementa la due funzioni che permettono di effettuare il resync di campi lookup rispetto alla definizione originale abilitanto frà l'altro i lookup cross site.
      La funzione è UpdateLookupReferences
      Accetta due parametri in ingresso:
      lookupfield - il field di tipo SPFieldLookup di cui volete modificare i riferimenti
      list - la SPlist da usare come nuova sorgente per il lookup.
      Viene usata una funzione di supporto (ReplaceXMLAttributeValue) che va a modificare lo schema XML del lookup field dato che l'unico modo per poter modificare un lookup è la modifica dello schema xml del field stesso. 

      Questa è la versione C#

      public voiid UpdateLookupReferences(SPFieldLookup lookupField, SPList list)
      {
      if (string.IsNullOrEmpty(lookupField.LookupList))
      {
      lookupField.LookupWebId = list.ParentWeb.ID;
      lookupField.LookupList = list.ID.ToString();
      }
      else
      {
      lookupField.SchemaXml = ReplaceXmlAttributeValue(lookupField.SchemaXml,
      "List", list.ID.ToString());
      lookupField.SchemaXml = ReplaceXmlAttributeValue(lookupField.SchemaXml,
      "WebId", list.ParentWeb.ID.ToString());
      }
      lookupField.Update(
      true);
      }

      private string ReplaceXmlAttributeValue(string xml, string attributeName, string value)
      {
      if (string.IsNullOrEmpty(xml))
      {
      throw new ArgumentNullException("xml");
      }
      if (string.IsNullOrEmpty(value))
      {
      throw new ArgumentNullException("value");
      }
      int indexOfAttributeName = xml.IndexOf(attributeName, StringComparison.CurrentCultureIgnoreCase);
      if (indexOfAttributeName == -1)
      {
      throw new ArgumentOutOfRangeException("attributeName", string.Format("Attribute {0} not found in source xml", attributeName));
      }
      int indexOfAttibuteValueBegin = xml.IndexOf('\"', indexOfAttributeName);
      int indexOfAttributeValueEnd = xml.IndexOf('\"', indexOfAttibuteValueBegin + 1);
      return xml.Substring(0, indexOfAttibuteValueBegin + 1) + value + xml.Substring(indexOfAttributeValueEnd);
      }

      Mentre questa è la versione VB.NET

      Public Sub UpdateLookupReferences(ByVal lookupField As SPFieldLookup,  ByVal list As SPList)
          If String.IsNullOrEmpty(lookupField.LookupList) Then
             lookupField.LookupWebId = list.ParebtWeb.ID
             lookupField.LookupList = list.ID.ToString()
          Else
             lookupField.SchemaXml = ReplaceXmlAttributeValue(lookupField.SchemaXml, "List", list.ID.ToString())
             lookupField.SchemaXml = ReplaceXmlAttributeValue(lookupField.SchemaXml,
      "WebId", list.ParentWeb.ID.ToString())
          End If
          lookupField.Update(True)
      End Sub

      Private Function ReplaceXmlAttributeValue(ByVal xml As String, ByVal attributeName As String, ByVal value As String) As String

         If
      String.IsNullOrEmpty(xml) Then
            Throw New ArgumentNullException("xml")
         End If
         If String.IsNullOrEmpty(value) Then
            Throw New ArgumentNullException("value")
         End If
         Dim indexOfAttributeName As Integer = xml.IndexOf(attributeName, StringComparison.CurrentCultureIgnoreCase)
         If indexOfAttributeName = -1 Then
            Throw New ArgumentOutOfRangeException("attributeName", String.Format("Attribute {0} not found in source xml", attributeName))
         End If
         Dim indexOfAttibuteValueBegin As Integer = xml.IndexOf(""""c, indexOfAttributeName)
         Dim indexOfAttributeValueEnd As Integer = xml.IndexOf(""""c, indexOfAttibuteValueBegin + 1)
         Return xml.Substring(0, indexOfAttibuteValueBegin + 1) & value & xml.Substring(indexOfAttributeValueEnd)

      End Function

      Appuntamento a domani per un'altra pillola.

      Categoria: Sharepoint
      martedì, 12 ott 2010 Ore. 01.06
      Copyright © 2002-2007 - Blogs 2.0
      dotNetHell.it | Home Page Blogs
      ASP.NET 2.0 Windows 2003