Showing posts with label CSOM. Show all posts
Showing posts with label CSOM. Show all posts

Monday, 19 January 2026

How to Get User Detail Programatically on ListItem Field

How to Get SPUser Detail Programatically on ListItem Field




 using (SPSite site = new SPSite("http://servername/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList List=web.Lists["Voucher"];
                   foreach(SPListItem ListItem in List.Items)
                   {
                       SPFieldUser ownerField =ListItem.Fields.GetField("ColumnName") as SPFieldUser;
                       SPFieldUserValue ownerValue = ownerField.GetFieldValue(ListItem[ownerField.Id].ToString()) as SPFieldUserValue;
                       SPUser owner = ownerValue.User;
                       string ownersEmail = owner.Email;
                       Console.WriteLine("user Name"+ListItem["ColumnName"].ToString());
                       Console.WriteLine("Users Email Id....."+ownersEmail.ToString());
                       Console.WriteLine("User Name .........." + owner.Name);
                       Console.WriteLine("User Id ............." + owner.ID);
                       Console.WriteLine("User Login........." + owner.LoginName);
                   
                       Console.Read();
                   }
                }
            }

Delete All item from sharepoint list using console application

Delete All item from  sharepoint on premise list using console application



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

namespace DeleteAllitem
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://win-0c3p6kepvck:100/kkk/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Department"];
                    int intcount = list.ItemCount;
                    for (int i = 0; i < intcount; i++)
                    {
                        list.Items.Delete(i);
                        Console.WriteLine("item deleted"+i.ToString());
                    }
                }
            }
        }
    }
}







How to Add Users to a group



Console.WriteLine("Enter a ; delimited list of domain\alias that need to be added:");
            string sAliases = Console.ReadLine(); //captures whatever the user entered
     
            string sAllContacts = "";

            using (SPSite site = new SPSite("http://win-0c3p6kepvck:100/kkk/"))
            {
                site.AllowUnsafeUpdates = true;
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    string[] aAliases = sAliases.Split(';');
                    foreach (string sAlias in aAliases)
                    {
                        SPUser user = web.EnsureUser(sAlias);

                        sAllContacts += user.ID.ToString() + ";#" + user.LoginName.ToString() + ";#";
                        if (sAllContacts.EndsWith(";#"))
                        {
                            sAllContacts = sAllContacts.Substring(0, sAllContacts.Length - 2);
                        }
                        SPList list = web.Lists["DemoList"];
                        SPListItem li = list.Items.Add();
                        li["Title"] = "jhjh";
                        li["parent"] = sAllContacts;
                        li["dname"] = sAllContacts;
                        li.Update();
                        Console.WriteLine("Done");
                    }
                    web.Update();

                }
            }