Showing posts with label Server Object Model. Show all posts
Showing posts with label Server Object Model. Show all posts

Monday, June 12, 2017

SharePoint List versioning using c#

We can also enable SharePoint List versioning using c#

using (SPSite spsite = new SPSite("http://win7/"))
{
SPWeb spweb = spsite.OpenWeb();
SPList list = spweb.Lists["CustomList"];
list.EnableVersioning = true;
list.Update();
}

Retrieve SPListItem versions using c#

using (SPSite spsite = new SPSite("http://win7/"))
{
SPWeb spweb = spsite.OpenWeb();
SPListItemCollection listitems = spweb.GetList("http://win7/Lists/CustomList").Items;

foreach (SPListItem listitem in listitems)
{
//listitem.Versions.RestoreByID(5);
foreach (SPListItemVersion versionItem in listitem.Versions)
{
Console.WriteLine("VersionId :" + versionItem.VersionId);
Console.WriteLine("VersionLabel :" + versionItem.VersionLabel);
Console.WriteLine("IsCurrentVersion :" + versionItem.IsCurrentVersion);
Console.WriteLine("ListItem.Title :" + versionItem.ListItem.Title);
Console.WriteLine("Created :" + versionItem.Created);
Console.WriteLine("CreatedBy :" + versionItem.CreatedBy);
Console.WriteLine("Level :" + versionItem.Level);
}
}
}

Retrieve SPListItem version changes using c#

using (SPSite spsite = new SPSite("http://win7/"))
{
SPWeb spweb = spsite.OpenWeb();
SPListItemCollection listitems = spweb.GetList("http://win7/Lists/CustomList").Items;

foreach (SPListItem listitem in listitems)
{
for (int i = 0; i < listitem.Versions.Count - 1; i++)
{
SPListItemVersion oldVersion = listitem.Versions[i];
SPListItemVersion latestVersion = listitem.Versions[i + 1];
foreach (SPField field in oldVersion.Fields)
{
if (field.ShowInVersionHistory == false)
{
continue;
}

if (latestVersion == null)
{
Console.WriteLine("  > {0} changed to \"{1}\"",
field.StaticName, oldVersion[field.StaticName]);
continue;
}

if (oldVersion[field.StaticName].Equals(latestVersion[field.StaticName]))
{
continue;
}

Console.WriteLine("  > {0} changed from \"{1}\" to \"{2}\"",
field.StaticName, latestVersion[field.StaticName], oldVersion[field.StaticName]);
}
}

}
}
Console.ReadLine();


Wednesday, July 13, 2016

How to change a date format in CAML

We have to add sharepoint Namespace  SPUtility


SPUtility.CreateISO8601DateTimeFromSystemDateTime(dateISO2)

Wednesday, June 8, 2016

Unique Constraints Programmatically SharePoint

SPSite site = new SPSite("http://peakfinders");
SPWeb web = site.OpenWeb();

SPList custList = web.Lists["Employee"];
SPField vtid = custList.Fields["VoterID"];

vtid.Indexed = true;
vtid.EnforceUniqueValues = true;
custPhone.Update();

How to add a user and multi users to the People picker field in a sharepoint list programmtically

Coding:

  if (spProjectListItem["USER"] != null)
      projectMemberValue = spProjectListItem["USER"].ToString();
      SPFieldUserValueCollection oFieldUserValueCollection = new SPFieldUserValueCollection(spDLRWeb,
                                                                                                                    spProjectListItem["ProjectMember"].ToString());
       spListItem["USER"] = oFieldUserValueCollection;

Wednesday, March 2, 2016

How to retrieving next 7 Days Due Date Tasks from SharePoint List

C# Coding:


DateTime strdate = (DateTime)DateTime.Today.AddDays(7);
            string strgetDate = strdate.Year + "-" + strdate.Month + "-" + strdate.Date.Day;

SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Projects"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<Where><Geq><FieldRef Name='DueDate' /><Value Type='DateTime'>"+strgetDate+"</Value></Geq></Where><OrderBy><FieldRef Name='DueDate' /></OrderBy>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                grdBind.DataSource = myColl.GetDataTable();
                grdBind.DataBind();
            }

How to dynamically Building HTML Table with SharePoint List

NameSapce:
Using System.Text

Coding:
strtxt append any kind text numerics.

public void bindata()
{
try{

SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        StringBuilder strtxt = new StringBuilder();
                        strtxt.Append("<table width='100%'>");
                        strtxt.Append("<tr>");
                        strtxt.Append("<td  bgcolor='#add8e6'>");
                        
                        strtxt.Append(" Name");
                       
                        strtxt.Append("</td>");
                        strtxt.Append("</tr>");
                        SPWeb currentWeb = SPContext.Current.Web;
                        SPList lstemp = currentWeb.Lists["Employee"];
                        SPListItemCollection myColl = lstemp.Items;
                        if (myColl.Count > 0)
                        {
                            foreach (SPListItem item in myColl)
                            {
                                string strTitle = string.Empty;
                                strTitle = item.Title.ToString();
                                strtxt.Append("<tr>");
                                strtxt.Append("<td>");
                                strtxt.Append(strTitle);
                                strtxt.Append("</td>");
                                strtxt.Append("</tr>");
                            }
                        }
                        strtxt.Append("</tr>");
                        writer.Write(strtxt);
                    });
                }
                catch (Exception ex)
                {
                    writer.Write(ex.ToString());
                }
            }
}

How to Insert New Item Into SharePoint List Programmatically

using (SPSite spSite = new SPSite(strSiteUrl, token))
{
spSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = spSite.OpenWeb())
{


SPList spList = spWeb.Lists["Enquiry"];

SPListItem spListItem = spList.Items.Add();

spListItem["Title"] = "Peak";
spListItem["Location"] = "India";


spWeb.AllowUnsafeUpdates = true;
 spListItem.Update();
}

}

Friday, February 12, 2016

When we get retrive People or Group field returning semicolon hash then Name

1;#Deepak Kumar


Get User name:

new SPFieldLookupValue(properties.ListItem["Staff Member"].ToString()).LookupValue;


Get User ID:

new SPFieldLookupValue(properties.ListItem["Staff Member"].ToString()).LookupId;

Using Split fuction:

properties.ListItem["Staff Member"].ToString();

You are going to be returned their userID and username, so it will always be returned separated by ;#.



properties.ListItem["Staff Member"].ToString().split(";#")[1];

Tuesday, December 15, 2015

How to update hyperlink filed value in SharePoint List using Server Object Model ?



SPListItem oiitem= oList.GetItemById(itemidvalue);
SPFieldUrlValue furl= new SPFieldUrlValue();

furl.URL = "peakfinders.blogspot.in";

furl.Description = "Peak Finders";

oiitem["SiteURL"] = furl ;

oiitem.update();