We are going to generate a Unique ID for each InfoPath form using a SharePoint list. We will then use this ID as the unique filename.
1. Create a SharePoint list with a key column:
2. Each time we load the InfoPath form, we will grab the key and then increment the key by 1:
XPathNavigator xNav = MainDataSource.CreateNavigator();
XPathNavigator nodeTermid = xNav.SelectSingleNode("my:myFields/my:TermID",
NamespaceManager);
if (nodeTermid.InnerXml == "0")
{
string termid = getTermID();
nodeTermid.SetValue(termid);
….
}
private string getTermID()
{
string termSite = ConfigurationManager.AppSettings["url_termsite"].ToString();
string termIDListName = ConfigurationManager.AppSettings["list_termid"].ToString();
string termIDKey = string.Empty;
SPSite mysite = null;
SPWeb site = null;
try
{
mysite = new SPSite(termSite);
site = mysite.RootWeb;
site.AllowUnsafeUpdates = true;
SPList list = site.Lists[termIDListName];
SPQuery query = new SPQuery();
string queryText = "<Where><Eq><FieldRef Name='{0}'" +
"/><Value Type='Text'>{1}</Value></Eq></Where>";
queryText = string.Format(queryText, "Title", "termid");
query.Query = queryText;
SPListItemCollection results = list.GetItems(query);
foreach (SPListItem item in results)
{
termIDKey = item["key"].ToString();
}
int newID = Convert.ToInt32(termIDKey) + 1;
UpdateFieldValues(termIDListName, "key", newID.ToString(), "Title", "termid");
return termIDKey;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mysite != null) mysite.Dispose();
if (site != null) site.Dispose();
}
}
public static void UpdateFieldValues(string listName, string attribute, string attrValue,
string filterField, string filterValue)
{
SPSite mysite = null;
SPWeb site = null;
try
{
mysite = new SPSite(ConfigurationManager.AppSettings["url_termsite"].ToString());
site = mysite.RootWeb;
site.AllowUnsafeUpdates = true;
SPList list = site.Lists[listName];
SPQuery query = new SPQuery();
string queryText = "<Where><Eq><FieldRef Name='{0}'" +
"/><Value Type='Text'>{1}</Value></Eq></Where>";
queryText = string.Format(queryText, filterField, filterValue);
query.Query = queryText;
SPListItemCollection results = list.GetItems(query);
foreach (SPListItem item in results)
{
item[attribute] = attrValue;
item.UpdateOverwriteVersion();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mysite != null) mysite.Dispose();
if (site != null) site.Dispose();
}
}
Tuesday, March 24, 2009
Subscribe to:
Posts (Atom)