Category Archives: Visual Studio

Using C# to read data from a SharePoint list using the SharePoint REST API

If you’re working with a C# application that is required to read information contained in a SharePoint list located on an external SharePoint farm, the SharePoint REST API can provide just the solution that you’re looking for.  Here is some sample code that you can use for accessing the information contained in that SharePoint list:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;

namespace Sample
{
    public class SharePointListReader
    {
        ...

        public List<SharePointListItem> GetAllSPListItems()
        {
            List<SharePointListItem> posts = new List<SharePointListItem>();
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://webapp/site/_api/web/lists/getbytitle('listName')/items?$select=id,Title");
            request.Method = "GET";
            request.Accept = "application/json;odata=verbose";
            request.ContentType = "application/json;odata=verbose";
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            WebResponse response = request.GetResponse();
            Data data = null;

            // Read the returned posts into an object that can be consumed by the calling application
            using (response)
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    try
                    {
                        string jSON = reader.ReadToEnd();
                        data = serializer.Deserialize(jSON);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("An error occurred when reading the list items from SharePoint: {0}; {1}", ex.Message, ex.StackTrace));
                    }
                }
            }
            foreach (SharePointListItem post in data.d.results)
            {
                posts.Add(post);
            }
            return posts;
        }
    }

    public class Data
    {
        public Results d { get; set; }
    }

    public class Results
    {
        public SharePointListItem[] results { get; set; }
    }

    public class SharePointListItem
    {
        public string id { get; set; }
        public string Title { get; set; }
    }
}

Error occurred in deployment step ‘Install app for SharePoint’: The provided App differs from another App with the same version and product ID

SharePoint-2013

Problem:
Cannot deploy a solution and getting the error: Error occurred in deployment step ‘Install app for SharePoint’: The provided App differs from another App with the same version and product ID.

Solution:
Open the AppManifest.xml in code view and generate a new guid for the app -> Change value of ProductID.

This task requires the application to have elevated permissions.

I start my Visual Studio 2010, create new SharePoint project and after giving project name and clicking OK I get this error:

ElevatedPrivVS2010

This task requires the application to have elevated permissions.

I already started VS2010 as administrator, but that’s not the solution yet.

I log to the server via Remote Desktop with my domain account. To be able to connect VS to SharePoint, my account needs to be added to local administrators group (get your admin/domain admin to do that).
Afterwards, I need to log out from Windows (thus ending my session) and log in again. Now, when I run Visual Studio as administrator, I can create new SharePoint project.

Regular Expressions Example (c#) – CSharp

csharp

The following example shows the use of Regular Expresssions in C#.This program has basic validation scripts for validation easily useable in all programs.

/*
<HowToCompile>
csc /r:System.Text.RegularExpressions.dll,System.dll Validation.cs
</HowToComplie>
*/
using System.Text.RegularExpressions;
using System;
class Validation
{
public static void Main()
{
String strToTest;
Validation objValidate=
new Validation();
Console.Write(“Enter a String to Test for Alphabets:”);
strToTest=Console.ReadLine();
if(objValidate.IsAlpha(strToTest))
{
Console.WriteLine(“{0} is Valid Alpha String”,strToTest);
}
else
{
Console.WriteLine(“{0} is not a Valid Alpha String”,strToTest);
}
}
// Function to test for Positive Integers.
public bool IsNaturalNumber(String strNumber)
{
Regex objNotNaturalPattern=
new Regex(“[^0-9]”);
Regex objNaturalPattern=
new Regex(“0*[1-9][0-9]*”);
return !objNotNaturalPattern.IsMatch(strNumber) &&objNaturalPattern.IsMatch strNumber);
}
// Function to test for Positive Integers with zero inclusive
public bool IsWholeNumber(String strNumber)
{
Regex objNotWholePattern=
new Regex(“[^0-9]”);
return !objNotWholePattern.IsMatch(strNumber);
}
// Function to Test for Integers both Positive & Negative
public bool IsInteger(String strNumber)
{
Regex objNotIntPattern=
new Regex(“[^0-9-]”);
Regex objIntPattern=
new Regex(“^-[0-9]+$|^[0-9]+$”);
return !objNotIntPattern.IsMatch(strNumber) &&
objIntPattern.IsMatch(strNumber);
}
// Function to Test for Positive Number both Integer & Real
public bool IsPositiveNumber(String strNumber)
{
Regex objNotPositivePattern=
new Regex(“[^0-9.]”);
Regex objPositivePattern=
new Regex(“^[.][0-9]+$|[0-9]*[.]*[0-9]+$”);
Regex objTwoDotPattern=
new Regex(“[0-9]*[.][0-9]*[.][0-9]*”);
return !objNotPositivePattern.IsMatch(strNumber) &&
objPositivePattern.IsMatch(strNumber) &&!objTwoDotPattern.IsMatch(strNumber);
}
// Function to test whether the string is valid number or not
public bool IsNumber(String strNumber)
{
Regex objNotNumberPattern=
new Regex(“[^0-9.-]”);
Regex objTwoDotPattern=
new Regex(“[0-9]*[.][0-9]*[.][0-9]*”);
Regex objTwoMinusPattern=
new Regex(“[0-9]*[-][0-9]*[-][0-9]*”);
String strValidRealPattern=”^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$”;
String strValidIntegerPattern=”^([-]|[0-9])[0-9]*$”;
Regex objNumberPattern =
new Regex(“(” + strValidRealPattern +”)|(” +
trValidIntegerPattern + “)”);
return !objNotNumberPattern.IsMatch(strNumber) &&!objTwoDotPattern.IsMatch
strNumber) && !objTwoMinusPattern.IsMatch(strNumber) && objNumberPattern.IsMatch(strNumber);
}
// Function To test for Alphabets.
public bool IsAlpha(String strToCheck)
{
Regex objAlphaPattern=
new Regex(“[^a-zA-Z]”);
return !objAlphaPattern.IsMatch(strToCheck);
}
// Function to Check for AlphaNumeric.
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern=
new Regex(“[^a-zA-Z0-9]”);
return !objAlphaNumericPattern.IsMatch(strToCheck);
}
}

There is another simple way to perform these validation think of it while the next article comes.

Installing Visual Studio – Ultimate 2013 with Blend

Visual Studio 2012 - Ultimate 1

To install Visual Studio Ultimate 2013 we are using the following Setup file:

SW_DVD5_VS_Ultimate_2013_English_##_##F_X##-####5

Right Click and Mount, Run “vs_ultimate” Application file as Administrator:

This Screen will Show up:

Visual Studio 2012 - Ultimate 1

Wait for the loading then the following screen will come:

Visual Studio 2012 - Ultimate 2

Note: I have changed the Path so the my Operating Systemdrive (C:\) does not get lot of Load.

Click “I agree” and then Next, then the following screen will come:

Visual Studio 2012 - Ultimate 3 new

Note: We can select what features we need from the above section of “Optional Features to Install”.

Click “Install, then the following screen will come:

Visual Studio 2012 - Ultimate 4 new

After Installation, then the following screen will came:

Visual Studio 2012 - Ultimate Error 5

I did a restart of the Server and AGAIN run the setup of Visual Studio and only one issue remained, the screen was displayed as following :

Visual Studio 2012 - Ultimate Error 7

To resolve issue of Microsoft Web Deploy 3.0 “A required certificate is not within its validity period when verifying against the current system clock or the timestamp in the signed file.”

I googled and found this Microsoft Link, It will download a windows installer file “MicrosoftFixit50531” just run it as Administrator.

Over view of the Error is :

These errors occur because of a failed timestamp comparison in the installer that is caused by an improperly signed Web Deploy Package.If you receive a warning after you successfully complete the installation of Visual Studio 2012, you can fix the problem by installing Web Deploy 3.0.

I did a restart of the Server and AGAIN run the setup of Visual Studio and all issues resolved, the screen was displayed as following :

Visual Studio 2012 - Ultimate 6

Now I clicked “LAUNCH”, following screen came:

Visual Studio 2012 - Ultimate 7 a

Now I clicked “Not now, maybe later”, following screen came:

Visual Studio 2012 - Ultimate 7 b

We have multiple options for “Theme” and “Development Settings”:

Visual Studio 2012 - Ultimate 7 c

Now I clicked “Start Visual Studio”, following screen came:

Visual Studio 2012 - Ultimate 8

It takes some time and we have our Visual Studio Ultimate 2013 “Start Page

Visual Studio 2012 - Ultimate 9

If You look closer to the top right there is a Flag for the “Notification Area”.

Visual Studio 2012 - Ultimate 10

Click “Notification Area” and the following screen appers.

Now you have successfully installed “Visual Studio Ultimate 2013”. Enjoy 😀