Adding new folder on SharePoint 2013 using C#

You can easily add a folder to your document library using the GUI but what if you are migrating from a different environment. This is when you need to write custom code to automatically create folders. First you will need to create a console project on the Visual Studio. I am using Visual Studio 2010. Then you will need to add a web reference on this project that points to your SharePoint 2013 web services. URL to SharePoint web services should be http://localhost/_vti_bin/Lists.asmx where you can replace the localhost with your server URL. Once the web reference has been added, you can use the code below to create a method on your class. You can call this method to add the new folder to your SharePoint Document Library.
public void CreateFolder(string folderLocation, string folderName){
ListsService.Lists listWebService = new ListsService.Lists(); // listWebService is the name given to the web reference
string xmlCommand;
XmlDocument doc = new XmlDocument();
xmlCommand = "<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" + folderLocation + folderName + "</Field><Field Name='ID'>New</Field></Method>";
XmlElement ele = doc.CreateElement("Batch");
ele.SetAttribute("OnError", "Continue");
ele.InnerXml = xmlCommand;
listWebService.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode node1 = listWebService.UpdateListItems("Document Library", ele); //"Document Library" should be replaced with the name for your Document Library.

Using View with Linked Server (Oracle) on Entity Framework (.NET)

One of the issue I came across on Entity Framework was pulling a view that consist of a linked server table (Oracle). When I added the view, I was getting the following error. "The table/view LINKED_SERVER_VIEW does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it." I needed this view as readonly. Only solution that worked was using ISNULL to define a primary key on the view. You will have to create a view like this for Entity Framework to understand the primary key. CREATE VIEW LINKED_SERVER_VIEW AS SELECT ISNULL(PRIMAYKEYCOLUMN, 0) as COL1, OTHERCOLUMN from LINKEDSERVER..SCHEMA.TABLENAME I believe the best approach is to actually fix the table or the model itself and add an actual primary key. However, sometime you will have to work on a database that a client does not want to change and you will have to deal with issues like this. Hope this solution works for you. If you have better solution, feel free to comment.

Protected Internal access modifiers on C#

Most of us have question on what is a protected internal modifiers. In short, it is a access modifier that can be accessed within the same assembly or by a class that inherits in a different assembly. For testing purposes, we will create an C# solution that has two projects. One of the project is a web application/console application and another is a class library. Instead of application you can create a class library if you wish. We will use the protected, internal and protected internal access modifiers to test the results. On the class library project, I have the following classes Class1 which contains 3 methods with protected, internal and protected internal access modifiers. Class2 does not inherit Class1 but it instantiates Class1 object "b" Class3 inherits Class1 and instantiates object "c" hence inheriting Class1 methods. "b" and "c" will try to access the methods on Class1. On the web application project, I will be using the namespace of class library project above to inherit Class1. Before I do this, I will need to compile class library project above and add the dll as a reference into the web application project. On this web application project, I created the fourth class called Class4. This class will inherit Class1 from the dll. I will try to access the methods on Class1 by creating an object "d". Below is the code Class Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
    public class Class1 //super class that contains the access modifiers and methods
        protected void testprotected() { }
        internal void testinternal() { }
        protected internal void testprotectedinternal() { }
    public class Class2 //different class on same assembly
        public void test2()
            Class1 b = new Class1();
            b.testprotected(); //Error 'ClassLibrary1.Class1.testprotected()' is inaccessible due to its protection level
    public class Class3 : Class1 // different class on same assembly that inherits class on same assembly
        public void test3()
            Class3 c = new Class3();
Web Application
using System.Linq;
using System.Web;
using ClassLibrary1;
namespace protectedinternaltest1
    public class Class4 : Class1 // different class that inherits class from different assembly
        public void test4()
            Class4 d = new Class4();
            d.testinternal(); //Error 'protectedinternaltest1.Class4' does not contain a definition for 'testinternal' and no extension method 'testinternal' accepting a first argument of type 'protectedinternaltest1.Class3' could be found (are you missing a using directive or an assembly reference?)
You will receive the errors(commented above) on the code because of encapsulation. Same Assembly, different class protected: not accessible because protected modifiers allows only sub class to access the protected method internal: accessible because it is within the same assembly protected internal: accessible because it is within the same assembly Same Assembly, sub class (inheritance) protected: accessible because it is a sub class of Class1 internal: accessible protected internal: accessible Different assembly, sub class (inheritance) protected: accessible because protected allows sub class to access the method. internal: inaccessible because it is not within the same class protected internal: accessible because method is called from sub class.

Convert Membership GUID to User Information

Here is an example of converting Membership userid to First Name and Last Name on Membership Profile. The way you will call this function ...
Response.write(convertGuidtoName(Guid "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));
public string convertGuidtoName(Guid g)
        MembershipUser user = Membership.GetUser(g) ; //
        string name = user.UserName; // get username from membership
        ProfileCommon userProfile = (ProfileCommon)
        ProfileCommon.Create(name, true);
        string x = string.Concat(userProfile.FirstName, " ", userProfile.LastName);
        return x;

Creating web service in VB.NET that outputs to JSON raw data

Here is a quick tutorial on creating a web service in VB.NET that output the data in JSON.
Imports System
Imports System.Web
Imports System.Collections
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class config
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function getConfigurationValueByKey(ByVal strKeyName As String) As String
        Dim sqlConnection As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
        Dim sqlCommand As SqlCommand = New SqlCommand("SELECT * FROM table1 WHERE column1 ='" + strKeyName + "';", sqlConnection)
        Dim sqlDataset As DataSet = New DataSet()
        Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
        Dim MArray()() As String = New String(sqlDataset.Tables(0).Rows.Count)() {}
        Dim i As Integer = 0
        For Each rs As DataRow In sqlDataset.Tables(0).Rows
            MArray(i) = New String() {rs("KeyName").ToString(), rs("ValueName").ToString()}
            i = i + 1
        Dim js As JavaScriptSerializer = New JavaScriptSerializer()
        Dim sJSON As String = js.Serialize(MArray)
        Return sJSON
    End Function
End Class
Output will be something like this.
  <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="">[["key1","value1"],["key2","value2"]]</string>

Display SQL result horizontally (comma separated) in MSSQL 2005

Here is a sql statement to display a column result horizontally. e.g. record1, record2, record3, record4, ... recordx
DECLARE @id_list VARCHAR(MAX) --varchar max works for SQL 2005 or above
		ELSE @id_list + ',' + CONVERT(VARCHAR,column1) END
		FROM table1		
SELECT @id_list