Category: SharePoint 2010

Get Workflow Status Programmatically in SharePoint Using Client Side Object Model C#

This post explains how to get SharePoint list item’s workflow status using Client Side Object Model programming, using the below code we can find workflow status id. Because workflow status field internally save the workflow status id only not as name,


List oList = clientContext.Web.Lists.GetByTitle("Documents");
ListItem item = oList.GetItemById(14);
clientContext.Load(item);
clientContext.ExecuteQuery();
Field workflowStatusField = item.ParentList.Fields.GetByTitle("WorkflowName");
clientContext.Load(workflowStatusField);
clientContext.ExecuteQuery();
var workflowStatusValue = item[workflowStatusField.InternalName];

In that code workflowStatusValue receive a status id as a list item workflow status. Here I’ve listed workflow status code and their status,

NotStarted = 0
FailedOnStart = 1
InProgress = 2
ErrorOccurred = 3
StoppedByUser = 4
Completed = 5
FailedOnStartRetrying = 6
ErrorOccurredRetrying = 7
ViewQueryOverflow = 8
Canceled = 15
Approved = 16
Rejected = 17

for testing I have created a workflow, named as “WorkflowName” and assigned to one document.

2016-02-12_0-37-37

In the below screenshot we can see that the code returns 2, which means workflow status is in-progress.

workflow

Feel free to contact me if you have any doubts and queries through comments.

Programmatically Add Excel Word Access Content Types SharePoint 2010

Create new project using visual studio “Empty SharePoint Project” template, and then select sandbox solution, and then add new web part, now we call below function to create new Content Template with particular document and add some site column, then its add one document library.

        private void ContentTypeAdder()
        {
            using (SPSite site = new SPSite(“http://win7”))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPContentType contentType = new SPContentType(web.ContentTypes[“Document”], web.ContentTypes, “SPTECHNET Time Sheet”);
                    web.ContentTypes.Add(contentType);
                    contentType.Group = “SPTECHNET Content Types”;
                    contentType.Description = “Demo content type”;
                    contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Address”)));
                    contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Office”)));
                    contentType.DocumentTemplate = “http://win7/Shared Documents/Test Dummy.docx”;
                    contentType.Update();
                    
                    SPList list = web.Lists[“Shared Documents”];
                    list.ContentTypesEnabled=true;
                    list.ContentTypes.Add(contentType);
                    list.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }

Image

Could not load type ‘Microsoft.Office.Server.Search.Administration.SearchContext’

Error

Could not load type ‘Microsoft.Office.Server.Search.Administration.SearchContext’ from assembly ‘Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’.

Err

Fix

Click Project properties, select Build tab and then select project platform x64

Fix

SharePoint ListItem Versioning and Changes in C#

If we need to enable SharePoint list versioning then navigate List Settings–>Versioning settings and then select radio button of create a version each time you edit an item in this list? Section, optionally limit the number of versions to retain is help to reduce DB size, SharePoint list does not support minor version and draft version.

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();

Deploy ASP.Net project within SharePoint

If you deploy asp.net project within SharePoint site, we can directly access spcontext in object model code. your asp.net project should developed framework 3.5, .framework 4.0 dose not support in this method.

Step 1:

Copy source folder and paste it on “C:\inetpub\wwwroot\wss\VirtualDirectories\<port>\”  

Step 2:

Open internet manager (inetmgr), Expand the SharePoint site then we can see our asp.net project folder , Right click the folder then click “convert as application” then click OK, browse any one page

https://sptechnet.files.wordpress.com/2011/07/iis.png

Step 3:

Now we get some errors like”Internal server Error”, that is duplicate problem, we need remove some sections and Add controls tags in web.config

Copy Or Move Documents One Document Library to Another Document Library in SharePoint 2010

Navigate Document Library –>Library Tab “Open with Explorer”,after open Windows Explorer then Copy or move Documents within your SharePoint then we can keep all metadata in the documents.

Restore Deleted Items From Recycle Bin in SharePoint 2010

Step 1

If we need change recycle bin setting then Navigate Central Administration –> Manage Web Application –>Select Web Application then select General Setting –>General Setting
here we can see the recycle bin setting.

Step 2

If we need restore deleted items from recycle bin then navigate Recycle Bin –> Site Collection Recycle Bin –> Deleted from end user Recycle Bin , here we can select and restore the deleted items .