Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, January 18, 2018

Export a Sharepoint list to a csv using C#.

Below is the code to export a Sharepoint List to a CSV file using C#.NET

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;

namespace export_sharepoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "<Site URL>" ;

            using (SPSite oSite = new SPSite(url))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPList oList = oWeb.Lists["<List Name>" ];

                    foreach (SPListItem oItem in oList.Items)
                    {
                         FileStream fs = new FileStream ( "C:\\Documents and Settings\\administrator.domain\\Desktop\\sites.txt", FileMode.Append, FileAccess.Write, FileShare .Read);
                         StreamWriter writer = new StreamWriter(fs);
                        // Access each item in the list... 
                        writer.Write(oItem[ "Site ID"]);
                        writer.Write( ",");
                        writer.Write(oItem[ "Column Name"]);
                        writer.Write( ",");

                        writer.Close();
                        fs.Close();
                    }

                }
            } 
        }
    }

Friday, January 5, 2018

Write to a file in CSharp

Quick and dirty way to write to a file in C#.

StreamWriter log = new StreamWriter(@c:\log.txt);
log.WriteLine(ErrorMessage);
log.WriteLine();
log.Close();

test test

Disable form repost on browser refresh

Here's a trick I found to prevent a form repost on browser refresh


        protected void Page_PreRender(object sender, EventArgs e)
        {
                     ViewState["Check_Page_Refresh"] = Session["Check_Page_Refresh"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Check_Page_Refresh"] = DateTime.Now.ToString();
            }
        }

protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
{
     <your main code you want to execute once on button click here>
}
}

Active Directory authentication using C#

bool authenticated;
using(PrincipalContext pc =newPrincipalContext(ContextType.Domain, domain.ToString()))
{
     authenticated = pc.ValidateCredentials(<user name>,<password>,ContextOptions.Negotiate);
}
if (authenticated)
{

}

Write to a file in CSharp

Quick and dirty way to write to a file in C#.

StreamWriter log = new StreamWriter(@c:\log.txt);
log.WriteLine(ErrorMessage);
log.WriteLine();
log.Close();

test test

Disable form repost on browser refresh

Here's a trick I found to prevent a form repost on browser refresh


        protected void Page_PreRender(object sender, EventArgs e)
        {
                     ViewState["Check_Page_Refresh"] = Session["Check_Page_Refresh"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Check_Page_Refresh"] = DateTime.Now.ToString();
            }
        }

protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
{
     <your main code you want to execute once on button click here>
}
}

Write to a file in CSharp

Quick and dirty way to write to a file in C#.

StreamWriter log = new StreamWriter(@c:\log.txt);
log.WriteLine(ErrorMessage);
log.WriteLine();
log.Close();

test test

Disable form repost on browser refresh

Here's a trick I found to prevent a form repost on browser refresh


        protected void Page_PreRender(object sender, EventArgs e)
        {
                     ViewState["Check_Page_Refresh"] = Session["Check_Page_Refresh"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Check_Page_Refresh"] = DateTime.Now.ToString();
            }
        }

protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
{
     <your main code you want to execute once on button click here>
}
}

Check if a user is in an AD group.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Security.Principal;

namespace test_adgroup
{
   class Program
    {
       static void Main(string[] args)
        {
           string username = "cykill";
           //string username = WindowsIdentity.GetCurrent().Name;

           PrincipalContext pc = new PrincipalContext(ContextType.Domain,"mydomainname");
           UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);

           if(up.IsMemberOf(pc,IdentityType.SamAccountName,"mygroup"))
            {
               Console.WriteLine("In Group");
            }
           else
            {
               Console.WriteLine("Not In Group");
            }

        }
    }
}

Caveat:  This does not work for the "Domain Users" group.  It always return false.  I don't know why.
Update:  Seems it doesn't work for large AD groups.

Write to a file in CSharp

Quick and dirty way to write to a file in C#.

StreamWriter log = new StreamWriter(@c:\log.txt);
log.WriteLine(ErrorMessage);
log.WriteLine();
log.Close();

test test

Disable form repost on browser refresh

Here's a trick I found to prevent a form repost on browser refresh


        protected void Page_PreRender(object sender, EventArgs e)
        {
                     ViewState["Check_Page_Refresh"] = Session["Check_Page_Refresh"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Check_Page_Refresh"] = DateTime.Now.ToString();
            }
        }

protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
{
     <your main code you want to execute once on button click here>
}
}

Check if a user is in an AD group with C#.

This functions checks weather a user account is in an AD group or not.  It will return true if the user account is in that group and false if not.

public static bool verifygroup(string s)
{
AppDomain myDomain = Thread.GetDomain();
myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal group = (WindowsPrincipal)Thread.CurrentPrincipal;
     if (group.IsInRole(s) == true)
     {
          return true;
     }
     else
     {
     return false;
     }
}

if(verifygroup("mydomain\\<ad group>") == true)
{
     do something here..
}


Tuesday, January 2, 2018

Write to a file in CSharp

Quick and dirty way to write to a file in C#.

StreamWriter log = new StreamWriter(@c:\log.txt);
log.WriteLine(ErrorMessage);
log.WriteLine();
log.Close();