Förra delen handlade om hur vi visade besökslistan i SharePoint. Låt oss nu titta på hur vi kan låta användare registrera besök från SharePoint.

Vad vi behöver göra är

  1. Visa ett formulär
  2. Posta formuläret till xVis API.

Formuläret

Det finns ett antal formulärsuppgifter som måste postas in i API:t. Eftersom inte manualen för detta är klar än (hrm…) så kommer denna info här:


<input type="hidden" value="add" id="action" name="action">
<input type="hidden" size="10" name="ddlDates" value="" id="ddlDates">
<input type="hidden" value="" size="20" name="tbName" id="tbName">
<input type="text" value="" size="20" name="tbCompany" id="tbCompany">
<input type="hidden" name="cbHideCompany" value="true" >
<input type="hidden" name="cbHideVisitor" value="true" >
<input type="hidden" name="ddlCat" id="ddlCat" value="0">
<input type="hidden" name="tbCardNo" id="tbCardNo" value="">
<input type="hidden" name="CustomField1Name" id="CustomField1Name" value="">
<input type="hidden" name="CustomField2Name" id="CustomField2Name" value="">
<input type="hidden" name="CustomField3Name" id="CustomField3Name" value="">
<input type="hidden" name="CustomField4Name" id="CustomField4Name" value="">
<input type="hidden" name="CustomField5Name" id="CustomField5Name" value="">
<input type="hidden" name="vInfo" id="vInfo" value="">
<input type="hidden" name="ddlHost" id="ddlHost">
<input type="hidden" name="cbWireLessUser" id="cbWireLessUser" value="false">
<input type="hidden" id="tbTime" name="tbTime" value="" >
<input type="hidden" value="" name="ddlDays" id="ddlDays">
<input type="hidden" name="cbRegisterIn" id="cbRegisterIn" value="true" >
<input type="hidden" name="cbFreq" id="cbFreq" value="true">
<input type="hidden" name="cbBooking" id="cbBooking" value="false">

Här är alla fält som måste bara med, angivna som hidden. Låt oss säga att man vill göra ett formulär som låter användaren ange namn, företag, värd och när besöket anländer och till när besöket stannar. I det här fallet gör vi det enkelt och visar datum i en dropdown. Skapa elementen som det passar dig bäst, nedan visas det i C#.

pnlForm.Controls.Add(new LiteralControl("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\" id=\"skit_toolBarTbltop\" class=\"ms-formtoolbar\">"));
pnlForm.Controls.Add(new LiteralControl("<tbody><tr>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-areaseparator\">"));
pnlForm.Controls.Add(new LiteralControl("<h2 class=\"ms-pagetitle\">Registrera besök</h2>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td>"));
pnlForm.Controls.Add(new LiteralControl("<table width=\"100%\" class=\"ms-formtable\">"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formlabel\">"));
pnlForm.Controls.Add(new LiteralControl("<strong>Besöket anländer</strong>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
ddlDates = new DropDownList();
ddlDates.ID = "ddlDates";
for (int i = 0; i < 31; i++)
{
string strVal =strDay.AddDays(i).ToShortDateString();
ddlDates.Items.Add(new ListItem(strVal,strVal));
}
ddlDates.SelectedValue = strDay.ToShortDateString();
ddlDates.AutoPostBack = true;
ddlDates.SelectedIndexChanged += new EventHandler(ddlDates_SelectedIndexChanged);
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formbody\">"));
pnlForm.Controls.Add(ddlDates);
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formlabel\">"));
pnlForm.Controls.Add(new LiteralControl("<strong>Namn</strong>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
tbName = new TextBox();
tbName.ID = "tbName";
tbName.CssClass = "ms-long";
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formbody\">"));
pnlForm.Controls.Add(tbName);
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formlabel\">"));
pnlForm.Controls.Add(new LiteralControl("<strong>Företag</strong>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
tbCompany = new TextBox();
tbCompany.ID = "tbCompany";
tbCompany.CssClass = "ms-long";
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formbody\">"));
pnlForm.Controls.Add(tbCompany);
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formlabel\">"));
pnlForm.Controls.Add(new LiteralControl("<strong>Besöksvärd</strong>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formbody\">"));
pnlForm.Controls.Add(new LiteralControl(GetHTML("http://demo.xvis.se/api/hosts.aspx?apikey=ABCDE&return=html&type=select")));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formlabel\">"));
pnlForm.Controls.Add(new LiteralControl("<strong>Besöket stannar till</strong>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
ddlDays = new DropDownList();
ddlDays.ID = "ddlDays";
for (int i = 0; i < 31; i++)
{
string strVal = strDay.AddDays(i).ToShortDateString();
ddlDays.Items.Add(new ListItem(strVal, i.ToString()));
}
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-formbody\">"));
pnlForm.Controls.Add(ddlDays);
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("</table>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("<tr>"));
pnlForm.Controls.Add(new LiteralControl("<td>"));
pnlForm.Controls.Add(new LiteralControl("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\" id=\"skit_toolBarTbltop\" class=\"ms-formtoolbar\">"));
pnlForm.Controls.Add(new LiteralControl("<tbody><tr>"));
pnlForm.Controls.Add(new LiteralControl("<td width=\"99%\" nowrap=\"\" class=\"ms-toolbar\"><img width=\"1\" height=\"18\" alt=\"\" src=\"/_layouts/images/blank.gif\"></td>"));
pnlForm.Controls.Add(new LiteralControl("<td nowrap=\"true\" class=\"ms-toolbar\">"));
pnlForm.Controls.Add(new LiteralControl("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td width=\"100%\" nowrap=\"\" align=\"right\">"));
Button btnSpara = new Button();
btnSpara.CssClass = "ms-ButtonHeightWidth";
btnSpara.Text = "OK";
btnSpara.Click += new EventHandler(btnSpara_Click);
pnlForm.Controls.Add(btnSpara);
pnlForm.Controls.Add(new LiteralControl("</td> </tr> </tbody></table>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("<td class=\"ms-separator\">&nbsp;</td>"));
pnlForm.Controls.Add(new LiteralControl("<td nowrap=\"true\" class=\"ms-toolbar\">"));
pnlForm.Controls.Add(new LiteralControl("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td width=\"100%\" nowrap=\"\" align=\"right\">"));
Button btnAvbryt = new Button();
btnAvbryt.CssClass = "ms-ButtonHeightWidth";
btnAvbryt.Text = "Cancel";
btnAvbryt.Click += new EventHandler(btnAvbryt_Click);
pnlForm.Controls.Add(btnAvbryt);
pnlForm.Controls.Add(new LiteralControl("</td> </tr> </tbody></table>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("</tbody></table>"));
pnlForm.Controls.Add(new LiteralControl("</td>"));
pnlForm.Controls.Add(new LiteralControl("</tr>"));
pnlForm.Controls.Add(new LiteralControl("</table>"));

Inga konstigheter, men vi använder också xVis API för att hämta de värdar som används av xVis


GetHTML("http://demo.xvis.se/api/hosts.aspx?apikey=ABCDE&return=html&type=select")

Denna funktion anropar APIt, och returnerar värdar i en select lista.

Nu har vi ett komplett formulär, och via eventet btnSpara_Click, tar vi hand om formuläret, och postar in det till API:t:

EnsureChildControls();
string today = DateTime.Today.Year + "-" + DateTime.Today.Month + "-" + DateTime.Today.Day;
string varden = "action=add&tbTime=&vInfo=";
varden += "&tbCardNo=&ddlDays=" + today + "&ddlDates=" + today + "&ddlCat=0";
varden += "&cbRegisterIn=false&cbWireLessUser=false&cbHideCompany&false&cbHideVisitor=false";
varden += "&CustomField5Name=&CustomField4Name=&CustomField3Name=&CustomField2Name=&CustomField1Name=";
varden += "&tbName=" + tbName.Text + "&tbCompany=" + tbCompany.Text + "&ddlHost=" + System.Web.HttpContext.Current.Request.Form["ddlHosts"];
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(varden);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://demo.xvis.se/api/visitoractions.aspx?apikey=ABCDE&type=addupdate");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
System.Web.HttpContext.Current.Response.Redirect("default.aspx");

Kort sagt, inget konstigt alls.

Planen är att exempelkoden i del 1 & 2 kommer finnas för nedladdning, som ett Visual Studio 2008 projekt, men vi ber att få återkomma med det.