Showing posts with label creating. Show all posts
Showing posts with label creating. Show all posts

Monday, 6 May 2013

Creating a ContentType with new Site Field in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces
Step 3: Write the following code.

using System;

//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace CreatingContentTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;
            clientContext.Load(site);
            clientContext.ExecuteQuery();

            //Getting content type name
            Console.Write("ContentType Name: ");
            string contentTypeName = Console.ReadLine();
        }
        static void CreateContentType(Web site, ClientContext clientContext, string contentTypeName)
        {
            ContentTypeCollection contentTypeCollection = site.AvailableContentTypes;
            clientContext.Load(contentTypeCollection);
            clientContext.ExecuteQuery();
            int count = 0;

            //Checking whether the ContentType already exists
            foreach (ContentType item in contentTypeCollection)
            {
                clientContext.Load(item);
                clientContext.ExecuteQuery();
                if (item.Name.ToUpper() == contentTypeName.ToUpper())
                    count++;
            }

            //If doesnot exist, create it
            if (count == 0)
            {
                Console.Write("SiteField Name: ");
                string siteFieldName = Console.ReadLine();
                string siteField =
                    "<Field Type = 'Text' " +
                    "Hidden = 'False' " +
                    "DisplayName = '" + siteFieldName + "' " +
                    "ResultType = 'Text' " +
                    "ReadOnly = 'False' " +
                    "Name = '" + siteFieldName + "' " +
                    "Group = 'Custom Columns'></Field>";
                Field field = site.Fields.AddFieldAsXml(siteField, false, AddFieldOptions.AddFieldToDefaultView);
                clientContext.ExecuteQuery();
                FieldLinkCreationInformation fieldLink = new FieldLinkCreationInformation();
                fieldLink.Field = field;

                //Creating ContentType
                ContentTypeCreationInformation contentType = new ContentTypeCreationInformation();
                contentType.Name = contentTypeName;
                contentType.Group = "SampleGroup";
                contentType.ParentContentType = contentTypeCollection[0].Parent;
                ContentType myContentType = site.ContentTypes.Add(contentType);

                //Adding fields to ContentType
                myContentType.FieldLinks.Add(fieldLink);
                field.Update();
                myContentType.Update(true);
                clientContext.ExecuteQuery();
                site.Update();
                Console.WriteLine("ContentType Created Successfully");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("ContentType already presents in the site.");
                Console.Write("Enter another ContentType name: ");
                string content = Console.ReadLine();
                CreateContentType(site, clientContext, content);
            }
        }
    }
}

Saturday, 2 February 2013

Creating Shortcut using JavaScript



WindowStyle Property:
intWindowStyle
Description
1
Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position.
3
Activates the window and displays it as a maximized window.
7
Minimizes the window and activates the next top-level window.

SpecialFolders:
Identifier
Folder Contents
AllUsersDesktop
Shortcuts that appear on the desktop for all users
AllUsersStartMenu
Shortcuts that appear on the Start menu for all users
AllUsersPrograms
Shortcuts that appear on the Programs menu for all users
AllUsersStartup
Shortcuts to programs that are run on startup for all users
Desktop
Shortcuts that appear on the desktop for the current user
Favorites
Shortcuts saved as favorites by the current user
Fonts
Fonts installed on the system
MyDocuments
Current users documents
NetHood
Objects that appear in Network Neighborhood
PrintHood
Printer links
Recent
Shortcuts to current users recently opened documents
SendTo
Shortcuts to applications that show up as possible send-to targets when a user right-clicks on a file in Windows Explorer
StartMenu
Shortcuts that appear in the current users start menu
Startup
Shortcuts to applications that run automatically when the current user logs on to the system
Templates
Application template files specific to the current user

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Creating Shortcut</title>
        <script type="text/javascript">
            function createShortcut() {
                var WshShell = new ActiveXObject("WScript.Shell");
                var strDesktop = WshShell.SpecialFolders("Desktop");
                var oShellLink = WshShell.CreateShortcut(strDesktop + "\\My Excel Shortcut.lnk");
                oShellLink.TargetPath = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.exe";
                oShellLink.WindowStyle = 1;
                oShellLink.Hotkey = "CTRL+SHIFT+E";
                oShellLink.IconLocation = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.exe, 0";
                oShellLink.Description = "Shortcut for Excel";
                oShellLink.WorkingDirectory = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\";
                oShellLink.Save();
                document.writeln("Shortcut for Excel Created Succesfully");
            }
        </script>
    </head>
    <body onload="javascript: createShortcut()">

    </body>
</html>

Note: This code only works in Internet Explorer.

Wednesday, 30 January 2013

Creating List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// SharePoint namespace
using Microsoft.SharePoint.Client;

namespace ListCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com/";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Creating List
            ListCreationInformation listCreationInformation = new ListCreationInformation();
            listCreationInformation.Title = "Students";
            listCreationInformation.TemplateType = (int)ListTemplateType.GenericList;
            List list = site.Lists.Add(listCreationInformation);

            //Creating Columns
            Field firstNameField = list.Fields.AddFieldAsXml(
                @"<Field Type='Text'
                         DisplayName='First Name' />", true, AddFieldOptions.DefaultValue);

            Field lastNameField = list.Fields.AddFieldAsXml(
                @"<Field Type='Text'
                         DisplayName='Last Name' />", true, AddFieldOptions.DefaultValue);

            Field ageField = list.Fields.AddFieldAsXml(
                @"<Field Type='Number'
                         DisplayName='Age' />", true, AddFieldOptions.DefaultValue);

            clientContext.ExecuteQuery();
            Console.WriteLine("List Created Successfully");
            Console.ReadLine();
        }
    }
}