Suppose you want to submit a form to SharePoint and then send e-mail with a
link to the form in the e-mail. People who receive the e-mail should be able
to just click the link and open the form. When you submit, you'll need to
escape the file name of the form so you can reuse it in the e-mail intro.
Here's some code to call from your OnClick handler. Please reply all with
any optimizations.
private bool performSubmit()
{
try
{
string fileName = getEscapedFileName();
string folderName = getFormLocation();
// Submit to SharePoint - you must define a "SharePoint submit"
data connection
DAVAdapter oSharePoint =
(DAVAdapter)thisXDocument.DataAdapters["SharePoint submit"];
oSharePoint.FileName = fileName;
oSharePoint.FolderURL = folderName;
oSharePoint.Submit();
// Submit to E-Mail - don't forget to define
EmailAdapter oEmail =
(EmailAdapter)thisXDocument.DataAdapters["Email submit"];
oEmail.To = "patrick_...@hotmail.com";
oEmail.Intro = string.Format("The following form has been updated:
{0}{1}", folderName, fileName);
oEmail.Submit();
return true;
}
catch(Exception ex)
{
thisXDocument.UI.Alert(ex.ToString());
return false;
}
}
private string getFormLocation()
{
string sTemp = thisXDocument.Solution.URI;
// HTTP address ending in ...\Forms\template.xsn
// First, check for this since it's more unique
int iPosition = sTemp.LastIndexOf("/Forms/");
if (-1 != iPosition)
{
// include the last slash or backslash in case
// we have to concatenate the filename later
return sTemp.Substring(0, iPosition + 1);
}
// TBD: need to gracefully degrade when there is no website
return "";
}
private string getEscapedFileName()
{
Uri solutionUri = new Uri(thisXDocument.Solution.URI);
string fileStr;
Uri fileUri;
// Check to see if the file is from an URL
int iPosition = thisXDocument.URI.LastIndexOf('/');
if(-1 != iPosition)
{
fileUri = new Uri(solutionUri,
thisXDocument.URI.Substring(iPosition+1), false);
fileStr = fileUri.AbsolutePath.ToString();
}
else
{
// Otherwise, recreate the name using the timestamp
DateTime dt = DateTime.Now;
string sVintageName =
string.Concat(getNodeValue("//my:myFields/my:UserName"), " ",
getNodeValue("//my:myFields/my:ExpenseTitle"));
// Uniquely name the file based on timestamp
fileUri = new Uri(solutionUri, string.Format("{0} -
{1:0000}{2:00}{3:00}-{4:00}{5:00}{6:00}.xml", sVintageName, dt.Year,
dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second), false);
// Getting AbsolutePath will escape the newly added file portion
// TBD: investigate easier way to get escaped name
fileStr = fileUri.AbsolutePath.ToString();
}
// Just return escaped filename for now
return fileStr.Substring(fileStr.LastIndexOf('/')+1);
}