Friday, February 22, 2013

Submit form with uploading document in documentlibrary and use of People Editor


Submit form with uploading document in documentlibrary and People Editor

.ascx file:



<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Doclib.ascx.cs" Inherits="UploadDocsUpdateList.Doclib.Doclib" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls"

             Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 

<style type="text/css">

/*dateFieldvalidupto*/

.ms-vb a:link,.ms-vb2 a:link,.ms-vb-user a:link{

/* [ReplaceColor(themeColor:"Hyperlink")] */ color:#0072BC;

text-decoration:none;

}

a:link{

/* [ReplaceColor(themeColor:"Hyperlink")] */ color:#0072BC;

text-decoration:none;

}

.ms-gb a,.ms-gb2 a{

/* [ReplaceColor(themeColor:"Accent3")] */ color:#003399;

}

    .style3

    {

        width: 100px;

    }

    INPUT.button{

       font-family:Verdana, Arial, Helvetica, sans-serif;

       font-size: 11px;

       font-weight:normal;

       color: #fff; 

       cursor: hand;

       background-color: #00578d;

       border:3px solid #0066a2;

}

.linedot {

       font-weight: normal;

       background-color: #e6eaf6;

}

   

    .style4

    {

        text-align: center;

    }

   

    </style>

  

 

   

    

    <div>

    <table width="918px" cellpadding="4" align="center">

    <tr>

    <td style="background-color:#e4e4e4">

    <table width="100%" cellspacing="1" style="background-color:#f4ffef; font-family:Verdana; font-size:11px;">

    <tr>

        <td colspan="4" style="background-color:#002455; color:#ffffff" height="25"

            valign="middle" class="style4"><strong>SUBMIT Form</strong></td>

    </tr>

    <tr>

        <td>&nbsp;</td>

        <td >&nbsp;</td>

        <td>&nbsp;</td>

        <td >&nbsp;</td>

    </tr>

    <tr><td style="background-color:#f4ffef">DOI</td><td>

        <asp:TextBox ID="txtissueddate" runat="server"></asp:TextBox>

        <cc1:CalendarExtender

            ID="CalendarExtender1" TargetControlID="txtissueddate" runat="server" Format="d/MM/yyyy">

        </cc1:CalendarExtender> </td>

       

        <td >Present Status</td>

        <td >

            <asp:DropDownList ID="drppresentstatus" runat="server"

                onselectedindexchanged="drppresentstatus_SelectedIndexChanged">

            </asp:DropDownList>

            </td>

      

    </tr>

       

    <tr>

        <td >Expiry</td>

       <td>

           <asp:TextBox ID="txtvalidupto" runat="server"></asp:TextBox><cc1:CalendarExtender ID="CalendarExtender2"

             TargetControlID="txtvalidupto"  runat="server" Format="d/MM/yyyy">

           </cc1:CalendarExtender>

       </td>

       

  

        <td >Date</td>

        <td> <asp:TextBox ID="txtDate" runat="server"></asp:TextBox><cc1:CalendarExtender ID="CalendarExtender3"

             TargetControlID="txtDate"  runat="server" Format="d/MM/yyyy">

           </cc1:CalendarExtender>

        </td>

        <td >&nbsp;</td>

        <td class="style3">&nbsp;</td>

    </tr>

    <tr>

        <td>Purpose of Issue</td>

        <td >

            <asp:TextBox ID="txtpurposeofissue" runat="server" TextMode="MultiLine"></asp:TextBox></td>

         <td >peoplepicker1</td>

        <td >

           <spuc:PeopleEditor ID="PeopleEditor1" runat="server" AllowEmpty="true" MultiSelect="true" SelectionSet="User" Width="200px" BorderStyle="Ridge" BorderColor="#CCCFFF"/></td>

    </tr>

    <tr>

        <td >

            peoplepicker2</td>

        <td  >

          <spuc:PeopleEditor ID="PeopleEditor2" runat="server" AllowEmpty="true" MultiSelect="true" SelectionSet="User" Width="200px" BorderStyle="Ridge" BorderColor="#CCCFFF"/></td>

      <td>Doc3</td>

        <td>

            <input type="file" id="InputFile3" runat="server" class="ms-fileinput" /></td>

   </tr>

   <tr>

        <td>Doc1</td>

        <td >

            <div> 

            <input type="file" id="InputFile" runat="server" class="ms-fileinput" />

            </div></td>

        <td>Doc2 </td>

        <td>

            <div>

            <input type="file" id="InputFile2" runat="server" class="ms-fileinput" /></div>

        </td>

    </tr>

    <tr>

        

          <td>&nbsp;</td>

          <td>&nbsp;</td>

    </tr>

    <tr>

        <td>&nbsp;</td>

        <td >&nbsp;</td>

        <td>&nbsp;</td>

        <td>&nbsp;</td>

    </tr>

    <tr class="linedot">

        <td colspan="4" style="text-align: center;">

            <asp:Button ID="Apply" runat="server" Text="Submit" onclick="Apply_Click" Height="26px" Width="86px" BackColor="#ED9857" BorderColor="#E67016" ForeColor="#002455" Font-Bold="true"/>&nbsp;&nbsp;&nbsp;

            <asp:Button ID="Clear" runat="server" Text="Clear" Width="86px" onclick="Clear_Click" Height="26px" BackColor="#ED9857" BorderColor="#E67016" ForeColor="#002455" Font-Bold="true"/>

       </td>

    </tr>

    <tr >

        <td colspan="4" style="height:2px;"></td>

    </tr>

    <tr>

        <td colspan="4" style="text-align: center" >

            <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>

        </td>

    </tr>

  </table>

   

   

    </td>

    </tr>

    </table>

     </div>

     

 

.ascx.cs file:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.IO;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Deployment;

using Microsoft.SharePoint.WebControls;

using Microsoft.Office.Server;

namespace UploadDocsUpdateList.Doclib

{

    public partial class Doclib : UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {

                SPSite site = SPContext.Current.Site;

                SPWeb web = site.OpenWeb();

                SPList list = web.Lists["Present Status List"];

                drppresentstatus.DataSource = list.Items;

               

                drppresentstatus.DataValueField = "Title";

                drppresentstatus.DataTextField = "Title";

                drppresentstatus.DataBind();

                drppresentstatus.Items.Insert(0, new ListItem("-Select-"));

            }

 

        }

 

        protected void Apply_Click(object sender, EventArgs e)

        {

                string dateofissue = string.Empty;

          

        

 

          

          string datevalid_upto = string.Empty;

              string dateofvalidupto_text = string.Empty;

              dateofvalidupto_text = txtvalidupto.Text;//txtdatevalidupto.Value.ToString();

 

         

                string Date = string.Empty;

 

                Date = txtDate.Text;

 

                string diffdate = string.Empty;

         

          

                DateTime Dateissue ;

                DateTime Validupto;

                DateTime Date1;

                TimeSpan datediff;

          

 

                string day_issue =string.Empty;

                string month_issue=string.Empty;

                string year_issue = string.Empty;

                string issue_date = string.Empty;// month_issue + "-" + day_issue + "-" + year_issue;

                string issued_date = string.Empty;// Dateissue.Date.ToString("MM/dd/yyyy");

                string validupto_date = string.Empty;// Validupto.Date.ToString("MM/dd/yyyy");

                string _date = string.Empty;//Date1.Date.ToString("MM/dd/yyyy");

 

                try

                {

                    SPSecurity.RunWithElevatedPrivileges(delegate()

                    {

                      //  using (SPSite site = new SPSite(siteName))

 

                        SPSite site = SPContext.Current.Site;

                      

                            SPWeb web = site.RootWeb;

                            {

                                web.AllowUnsafeUpdates = true;

                                var fStream = InputFile.PostedFile.InputStream;

                                string fileName=InputFile.PostedFile.FileName;

                               // string docName = filename;

var contents = new byte[fStream.Length];

string docname1 = string.Empty;

if (fileName != "")

{

    fStream.Read(contents, 0, (int)fStream.Length);

    fStream.Close();

    var listd = web.Lists["Doc1"];//st.GetList([TARGET LIBRARY URL HERE]);

    var destinationFolder = listd.RootFolder;

    var strDestUrl = destinationFolder.Url + "/" + fileName;

 

     string str = null;

    string[] strArr = null;

    string text = string.Empty;

 

    str = strDestUrl.ToString();

    string strspecialchar = "\\";

    if (str.Contains(strspecialchar))

    {

        char[] splitchar = { '\\' };

        strArr = str.Split(splitchar);

        for (int i = 0; i <= strArr.Length - 1; i++)

        {

            text = strArr[strArr.Length - 1].ToString();

            // MessageBox.Show(strArr[count]);

        }

        string desturl = destinationFolder.Url + "/" + text;

        var filen = destinationFolder.Files.Add(desturl, contents, true);

       //filen.Item["FIELD ITERNAL NAME"] = "[DATA]";

        filen.Item.SystemUpdate(false);

        docname1 = text;

    }

    else

    {

        var filen = destinationFolder.Files.Add(strDestUrl, contents, true);

       // filen.Item["FIELD ITERNAL NAME"] = "[DATA]";

        filen.Item.SystemUpdate(false);

        docname1 = fileName.ToString();

    }

}

  

  

           

 

var fStream2 = InputFile2.PostedFile.InputStream;

string fileName2 = InputFile2.PostedFile.FileName;

// string docName = filename;

var contents2 = new byte[fStream2.Length];

string docnam2 = string.Empty;

if (fileName2 != "")

{

    fStream2.Read(contents2, 0, (int)fStream2.Length);

    fStream2.Close();

    var listd2 = web.Lists["Doc2"];//st.GetList([TARGET LIBRARY URL HERE]);//Scan of POA

    var destinationFolder2 = listd2.RootFolder;

    var strDestUrl2 = destinationFolder2.Url + "/" + fileName2;

    string str = null;

    string[] strArr = null;

    string text = string.Empty;

 

    str = strDestUrl2.ToString();

    string strspecialchar = "\\";

    if (str.Contains(strspecialchar))

    {

        char[] splitchar = { '\\' };

        strArr = str.Split(splitchar);

        for (int i = 0; i <= strArr.Length - 1; i++)

        {

            text = strArr[strArr.Length - 1].ToString();

            // MessageBox.Show(strArr[count]);

        }

        string desturl = destinationFolder2.Url + "/" + text;

        var filen2 = destinationFolder2.Files.Add(desturl, contents2, true);

      //  filen2.Item["FIELD ITERNAL NAME"] = "[DATA]";

        filen2.Item.SystemUpdate(false);

        docnam2 = text;

    }

    else

    {

        var filen2 = destinationFolder2.Files.Add(strDestUrl2, contents2, true);

     //  filen2.Item["FIELD ITERNAL NAME"] = "[DATA]";

        filen2.Item.SystemUpdate(false);

        docnam2 = text;

    }

  

}

 

var fStream3 = InputFile3.PostedFile.InputStream;

string fileName3 = InputFile3.PostedFile.FileName;

// string docName = filename;

var contents3 = new byte[fStream3.Length];

string docnam3 = string.Empty;

if (fileName3 != "")

{

    fStream3.Read(contents3, 0, (int)fStream3.Length);

    fStream3.Close();

    var listd3 = web.Lists["Doc3"];//st.GetList([TARGET LIBRARY URL HERE]);//Scan of POA

    var destinationFolder3 = listd3.RootFolder;

    var strDestUrl3 = destinationFolder3.Url + "/" + fileName3;

    string str = null;

    string[] strArr = null;

    string text = string.Empty;

 

    str = strDestUrl3.ToString();

    string strspecialchar = "\\";

    if (str.Contains(strspecialchar))

    {

        char[] splitchar = { '\\' };

        strArr = str.Split(splitchar);

        for (int i = 0; i <= strArr.Length - 1; i++)

        {

            text = strArr[strArr.Length - 1].ToString();

            // MessageBox.Show(strArr[count]);

        }

        string desturl = destinationFolder3.Url + "/" + text;

        var filen3 = destinationFolder3.Files.Add(desturl, contents3, true);

        //  filen2.Item["FIELD ITERNAL NAME"] = "[DATA]";

        filen3.Item.SystemUpdate(false);

        docnam3 = text;

    }

    else

    {

        var filen3 = destinationFolder3.Files.Add(strDestUrl3, contents3, true);

        //  filen2.Item["FIELD ITERNAL NAME"] = "[DATA]";

        filen3.Item.SystemUpdate(false);

        docnam3 = text;

    }

 

}

 

 

                                SPList list = web.Lists["Submit Form List"];

                                SPQuery query = new SPQuery();

                                query.Query = "<Query></Query>";

                                SPListItemCollection itemcoll = list.GetItems(query);

                                int count = itemcoll.Count + 1;

                                string slno = count.ToString();

                                web.AllowUnsafeUpdates = true;

                                SPListItem item = list.Items.Add();

                                item["S. No."] = slno;

//peoplepicker//

                                SPFieldUserValueCollection srvcRepCol = new SPFieldUserValueCollection();

 

                                int index = 0;

 

                                for (index = 0; index <= this.PeopleEditor1.ResolvedEntities.Count - 1; ++index)

                                {

 

                                    PickerEntity objEntity = (PickerEntity)this.PeopleEditor1.ResolvedEntities[index];

 

                                    srvcRepCol.Add(new SPFieldUserValue(web, Convert.ToInt32(objEntity.EntityData["SPUserID"]), objEntity.Key));

 

                                }

 

                                //Assign the users to list item column

 

                                item["peoplepicker1"] = srvcRepCol;

 

 

 

                                //Set the value of  users to people editor

 

                                if (item["peoplepicker1"] != null)

                                {

 

                                    string users = string.Empty;

 

                                    //To get the value from sharepoint list item and storing it in a  SPFieldUserValueCollection.

 

                                    SPFieldUserValueCollection userCol = new SPFieldUserValueCollection(web, item["peoplepicker1"].ToString());

 

                                    foreach (SPFieldUserValue usrItm in userCol)

                                    {

 

                                        users += usrItm.User.ToString() + ",";

 

                                    }

 

 

                                    if (index != 0 )

                                    { this.PeopleEditor1.CommaSeparatedAccounts = users.Remove(users.LastIndexOf(","), 1); }

 

                                }

                                //Set the value of  users to people editor

 

                                //peopleeditor2//

 

 

                                SPFieldUserValueCollection srvcRepColppl2 = new SPFieldUserValueCollection();

 

                                int index2 = 0;

 

                                for (index2 = 0; index2 <= this.PeopleEditor2.ResolvedEntities.Count - 1; ++index2)

                                {

 

                                    PickerEntity objEntity2 = (PickerEntity)this.PeopleEditor2.ResolvedEntities[index2];

 

                                    srvcRepColppl2.Add(new SPFieldUserValue(web, Convert.ToInt32(objEntity2.EntityData["SPUserID"]), objEntity2.Key));

 

                                }

 

                                //Assign the users to list item column

 

                                item["peoplepicker2"] = srvcRepColppl2;

 

 

 

                                //Set the value of  users to people editor

 

                                if (item["peoplepicker2"] != null)

                                {

 

                                    string users2 = string.Empty;

 

                                    //To get the value from sharepoint list item and storing it in a  SPFieldUserValueCollection.

 

                                    SPFieldUserValueCollection userCol2 = new SPFieldUserValueCollection(web, item["peoplepicker2"].ToString());

 

                                    foreach (SPFieldUserValue usrItm2 in userCol2)

                                    {

 

                                        users2 += usrItm2.User.ToString() + ",";

 

                                    }

 

 

                                    if (index2 != 0)

                                    { this.PeopleEditor2.CommaSeparatedAccounts = users2.Remove(users2.LastIndexOf(","), 1); }

 

                                }

 

                          

 

                                if (txtissueddate.Text != "")

                                {

                                    Dateissue = Convert.ToDateTime(txtissueddate.Text);

 

                                    issued_date = Dateissue.Date.ToString("MM/dd/yyyy");

                                    item["DOI"] = Convert.ToString(issued_date);

                                }

                                else

                                { item["DOI"] = null; }

                                if (txtvalidupto.Text != "" && txtissueddate.Text != "")

                                {

                                  Dateissue = Convert.ToDateTime(txtissueddate.Text);

                                    Validupto = Convert.ToDateTime(txtvalidupto.Text);

                                    validupto_date = Validupto.Date.ToString("MM/dd/yyyy");

                                    item["Expiry"] = Convert.ToString(validupto_date);

                               

                              datediff= Validupto.Subtract(Dateissue);

                                   diffdate = datediff.Days.ToString();

                                    item["Validity Period"] = diffdate;

                                }

                                else if (txtvalidupto.Text != "" && txtissueddate.Text == "")

                                {

                                    //Dateissue = Convert.ToDateTime(txtissueddate.Text);

                                    Validupto = Convert.ToDateTime(txtvalidupto.Text);

                                    validupto_date = Validupto.Date.ToString("MM/dd/yyyy");

                                    item["Expiry"] = Convert.ToString(validupto_date);

 

                                    //datediff = Validupto.Subtract(Dateissue);

                                   // diffdate = datediff.Days.ToString();

                                    item["Validity Period"] = null;

                                }

                                else { item["Expiry"] = null; }

                                            

                           

                            

                              if (txtDate.Text!="")

                              {

                                  Date1 = Convert.ToDateTime(txtDate.Text);

                                 _date =Date1.Date.ToString("MM/dd/yyyy");

                                  item["Date"] = _date;

                              }

                              else{ item["Date"] = null; }

                              if (drppresentstatus.SelectedItem.Text != "-Select-") { item["Present Status"] = drppresentstatus.SelectedItem.Text; }

                              else { item["Present Status"] = ""; }

 

 

                              if (docname1 != "")

                              {

 

                                  SPFieldUrlValue hyper = new SPFieldUrlValue();

                                  hyper.Description = docname1; hyper.Url = "http://webapplication/Doc1/" + docname1;//internal name of Doc1 library

                                  //  item["Title"] = "New item";

                                  item["Doc1 link"] = hyper; item.Update();

 

                                  item["Doc1 link2"] = "http://webapplication/Doc1/" + docname1;

 

                                  item["DocumentNamedoc1"] = docname1;

                              }

                               

                              if (docnam2 != "")

                              {

 

 

                                  item["Doc2"] = "http://webapplication/Doc2/" + docnam2;

 

                                  SPFieldUrlValue hyper2 = new SPFieldUrlValue();

                                  hyper2.Description = docnam2; hyper2.Url = "http://webapplication/Doc2/" + docnam2;

 

                                  item["Doc2 link"] = hyper2; item.Update();

                                  item["DocumentNamedoc2"] = docnam2;

                              }

 

                              if (docnam3 != "")

                              {

                                  SPFieldUrlValue hyper = new SPFieldUrlValue();

                                  hyper.Description = docnam3; hyper.Url = "http://webapplication/Doc3/"  +docnam3;

 

                                  item["Doc3 link"] = hyper; item.Update();

 

                                  item["Doc3 link1"] = "http://webapplication/Doc3/" + docnam3;

 

                                

                                  item["DocumentNameDoc3"] = docnam3;

                              }

 

                             // item.Update();

                              //item.SystemUpdate();

                              if (index != 0 && index2 != 0)

                              {

                                  if (txtissueddate.Text == "" && txtvalidupto.Text == "" && txtDate.Text == "") { lblmsg.Text = "Please,select DOI and Expiry."; }

                                  else if (txtissueddate.Text == "" && txtvalidupto.Text == "" && txtDate.Text != "") { lblmsg.Text = "Please, enterDOI and Expiry "; }

                                  else if (txtissueddate.Text == "" && txtvalidupto.Text != "" && txtDate.Text != "")

                                  {

 

                                      lblmsg.Text = "Please,select Date of Issue.";

                                  }

                                  else if (txtissueddate.Text != "" && txtvalidupto.Text == "" && txtDate.Text != "")

                                  {

                                      DateTime Date_issue = Convert.ToDateTime(txtissueddate.Text);

                                      DateTime Date_superced = Convert.ToDateTime(txtDate.Text);

                                      TimeSpan _superced = Date_issue.Subtract(Date_superced);//Datepoa_superced.Subtract(Valid_upto);

                                      int diff_superced = _superced.Days;

                                      if (diff_superced >= 0)

                                      {

                                          item.Update(); lblmsg.Text = "Thanks for Submission.";

                                      }

                                      else { lblmsg.Text = " DOI should be greater than Date"; }

 

                                     // lblmsg.Text = "Please,select DOI.";

                                  }

                                  else if (txtissueddate.Text != "" && txtvalidupto.Text != "" && txtDate.Text == "")

                                  {

                                      DateTime Date_issue = Convert.ToDateTime(txtissueddate.Text);

                                      DateTime Valid_upto = Convert.ToDateTime(txtvalidupto.Text);

                                      TimeSpan date_diff = Valid_upto.Subtract(Date_issue);

                                      int diff_date = date_diff.Days;

                                      if (diff_date >= 0)

                                      {

                                          item.Update(); lblmsg.Text = "Thanks for Submission.";

                                      }

                                      else { lblmsg.Text = "Expiry should be greater than DOI"; }

                                  }

                                  else if (txtissueddate.Text != "" && txtvalidupto.Text != "" && txtDate.Text != "")

                                  {

 

                                      DateTime Date_issue = Convert.ToDateTime(txtissueddate.Text);

                                      DateTime Valid_upto = Convert.ToDateTime(txtvalidupto.Text);

                                      DateTime Date_superced = Convert.ToDateTime(txtDate.Text);

                                      TimeSpan date_diff = Valid_upto.Subtract(Date_issue);

 

                                      int diff_date = date_diff.Days;

                                      TimeSpan _superced = Date_issue.Subtract(Date_superced);//Date_superced.Subtract(Valid_upto);

                                      int diff_superced = _superced.Days;

                                      if (diff_date >= 0 && diff_superced >= 0)

                                      {

                                          item.Update(); lblmsg.Text = "Thanks for Submission.";

                                      }

 

 

                                      else { lblmsg.Text = " DOI should be greater than Date"; }

                                  }

                              }

                              else { lblmsg.Text = "Please,enter valid users in PeoplePicker1 and PeoplePicker2 ."; }

                            }

                    

                            }

                        );

               

              

                }

                catch (Exception ex)

                {

                    lblmsg.Text = ex.Message;

                }

            }

 

     

 

       

 

        protected void Clear_Click(object sender, EventArgs e)

        {

            lblmsg.Text = "";

          

           // txtvalidupto.Text = "";

            //txtDate.Text = "";

            Server.Transfer("/SitePages/abcd.aspx");

        }

 

        protected void drppresentstatus_SelectedIndexChanged(object sender, EventArgs e)

        {

 

        }

        }

 

     

     

    }