mercredi 29 janvier 2014

Trier une liste d'objets selon un critère précis C#

    if (infosLetterList != null && infosLetterList.Count > 0)
    {
        infosLetterList.Sort(delegate(InfoLetter a1, InfoLetter a2) { return string.Compare(a1.CentreInteret, a2.CentreInteret); });
    }

lundi 27 janvier 2014

Session lost problem after Response.Redirect

If you create a session like this and redirect the user to some other page, the session will lost.

Session["UserId"] = "User1";
Response.Redirect("YourPage.aspx");

This is because of the working of session and Response.Redirect, Lets go through.

" When you create a new session (that is, the first time you write to a Session variable), ASP.NET sets a volatile cookie on the client that contains the session token. On all subsequent requests, and as long as the server session and the client cookie have not expired, ASP.NET can look at this cookie and find the right session.
Now, what Redirect does is to send a special header to the client so that it asks the server for a different page than the one it was waiting for. Server-side, after sending this header, Redirect ends the response. This is a very violent thing to do. Response.End actually stops the execution of the page wherever it is using a ThreadAbortException.
What happens really here is that the session token gets lost in the battle.
There are a few things you can do to solve this problem.
First, in the case of the forms authentication, we already provide a special redirect method: FormsAuthentication.RedirectFromLoginPage. This method is great because, well, it works, and also because it will return the user to the page he was asking for in the first place, and not always default. This means that the user can bookmark protected pages on the site, among other things. " - Bertrand Le Roy
Another thing you can do is use the overloaded version of Redirect:

Response.Redirect("YourPage.aspx", false);

This does not abort the thread and thus conserve the session token. Actually, this overload is used internally by RedirectFromLoginPage.

mardi 21 janvier 2014

Créer son premier Projet Sharepoint

On commence par créer un empty sharepoint project

ensuite on met l'adresse du site dans lequel on va déployer:

et pour finir le résultat sera le suivant:

et puis on créera notre 1ère webpart clique droit sur le projet Ajouter -> Nouvel élément -> Visual WebPart et on lui donne un nom :

Ensuite on ouvre le sharepoint designer et on créé une page de webpart et on associe notre webpart qu'on vient de créer. pour plus de détails n'hésitez pas à laisser des commentaires.

Générer un excel avec C#

   public void FichierExcel(List<BeanTelechargementAppelOffre> DemandesBeans)
        {


            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.Charset = "";
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"" +"ListeEntreprises.xls" + "\"");

            Table tbl = new Table() { BorderWidth = new Unit(1) };
            TableHeaderRow th = new TableHeaderRow();
            th.Font.Bold = true;
            th.Cells.Add(new TableCell() { Text = "Liste des Personnes", ColumnSpan = 4, HorizontalAlign = HorizontalAlign.Center, BackColor = Color.DarkBlue, ForeColor = Color.White });
            tbl.Rows.Add(th);

            th = new TableHeaderRow();
            th.Font.Bold = true;

            th.Cells.Add(new TableCell() { Text = "Nom ou raison sociale", BackColor = Color.DarkBlue, ForeColor = Color.White, BorderWidth = new Unit(1) });
            th.Cells.Add(new TableCell() { Text = "Les pi&egrave;ces t&eacute;l&eacute;charg&eacute;es", BackColor = Color.DarkBlue, ForeColor = Color.White, BorderWidth = new Unit(1) });
            th.Cells.Add(new TableCell() { Text = "Email de la personne", BackColor = Color.DarkBlue, ForeColor = Color.White, BorderWidth = new Unit(1) });
            th.Cells.Add(new TableCell() { Text = "Date de la consultation", BackColor = Color.DarkBlue, ForeColor = Color.White, BorderWidth = new Unit(1) });

            tbl.Rows.Add(th);
            foreach (BeanTelechargementAppelOffre bean1 in DemandesBeans)
            {

                var tr = new TableRow();
                tr.Cells.Add(new TableCell() { Text = "" + bean1.NomPersonne, BorderWidth = new Unit(1) });
                tr.Cells.Add(new TableCell() { Text = bean1.TypeFichier, HorizontalAlign = HorizontalAlign.Center, BorderWidth = new Unit(1) });
                tr.Cells.Add(new TableCell() { Text = bean1.EmailPersonne, HorizontalAlign = HorizontalAlign.Center, BorderWidth = new Unit(1) });
                tr.Cells.Add(new TableCell() { Text = Convert.ToDateTime(bean1.DateConsultation).ToShortDateString(), HorizontalAlign = HorizontalAlign.Center, BorderWidth = new Unit(1) });
                tbl.Rows.Add(tr);
            }
            System.Text.StringBuilder stb = new System.Text.StringBuilder();
            System.IO.StringWriter sw = new System.IO.StringWriter(stb);
            HtmlTextWriter textWriter = new HtmlTextWriter(sw);
            tbl.RenderControl(textWriter);
            response.Write(stb.ToString());
            response.End();
        }