Monthly Archives: August 2013

SharePoint 2013 Create List Definition

Today i am going to post how to create list definition in SharePoint 2013 via Visual Studio SharePoint Solution

1) Add Site Column Module in your SP Solution

SiteColumn
Click to add Module Note: you will see visual studio automatically adds new feature for this module and i have renamed to SiteColumns.
you will see

SiteColumnAdded


1.1) Adding Site Columns in SiteColumns.xml, Student Name(Text), RollNumber(Number), Subjects(Choice)
 
Columns

2) Add Content Type Module as we did in above, i named it “ContentTypeModule” also add new feature by right clicking on Feature select add and name it ContentTypesFeature, finally add Content Type module in this feature.

feature

 
* Enter your Column name in your content type after placing cursor in click here to add a column, i have added above columns in content type see

ContentType

3) Add List Module in SP Solution

i have added list with name “StudentList” also added a seperate feature for lists named “ListsFeature” and finally added lists items in the lists feature, always add first list definition then list instance

List

FINAL THING
Double click on your List and click on add content type button, enter your content type name there and all the columns in that content will gets added as you can see below figure
final

Thats it 🙂

SharePoint Different ways to export web part

***** Exporting simple webpart
1- place any webpart on your page and click on down arrow button as you can see
1

2- Note: There is no option to export, click on Edit Web Part and choose export mode = Export All Data from Advance navigation section. click ok.
– now you can see

2

3- Click on Export and place your exported code in your site page.xml e.g

4- Site page created successfully, you can check in your pages library.

***** Exporting Content Editor webpart

here is complete detail
http://muhammadkazim.wordpress.com/2013/08/21/exporting-web-app-parts-content-editor-app-and-visual-web-part/

generic function in c# to create caml query for search

we have created a generic function which is used to create search query for us as follows

public static string Is_Active = “1”;
public const string qry_Title = @”{1}”;
public const string qry_Event = @”{1}”;
public const string qry_PictureCategory = @”{1}”;

string searchQuery = string.Empty;
searchQuery = CreateSearchQuery();

private string CreateSearchQuery()
{
const string AND_OPERATOR = “And”;
StringBuilder queryText = new StringBuilder(IS_Public);

if (!string.IsNullOrEmpty(txtTitle.Text))
{
AddToWhereClause(queryText, string.Format(qry_Title, Lists.PictureGallery.Title, txtTitle.Text), AND_OPERATOR);
//AddToWhereClause(queryText, string.Format(YourQueryText, ListFieldName, FieldValue), OPERATOR);
}
if (!string.IsNullOrEmpty(txtEvent.Text))
{
AddToWhereClause(queryText, string.Format(qry_Event, Lists.PictureGallery.Event, txtEvent.Text), AND_OPERATOR);
}
if (ddlCategory.SelectedItem.Text != “All”)
{
AddToWhereClause(queryText, string.Format(qry_PictureCategory, Lists.PictureGallery.Category, ddlCategory.SelectedItem.Text), AND_OPERATOR);
}
return Convert.ToString(queryText);
}

public void AddToWhereClause(StringBuilder whereClause, string clauseToAdd, string operatorName)
{
if (string.IsNullOrEmpty(clauseToAdd)) return;

const string WHERE_START_TAG = “”;
const string WHERE_END_TAG = “”;
string whereClauseText = whereClause.ToString();

whereClause = whereClause.Insert(whereClauseText.IndexOf(WHERE_END_TAG), string.Format(“”, operatorName));
whereClause = whereClause.Insert(whereClauseText.IndexOf(WHERE_START_TAG) + WHERE_START_TAG.Length, string.Format(“{1}”, operatorName, clauseToAdd));
}

Sharepoint create and open page as popup with querystring

function OpenDownloadRequestForm(FileUrl, ItemID) {
var layoutsUrl = SP.Utilities.Utility.getLayoutsPageUrl(‘/DownloadRequests.aspx?isdlg=1&FileUrl=’ + FileUrl+’&ItemId=’ + ItemID);
var options = {
title: “Download file”,
width: 475,
height: 500,
url: layoutsUrl
}

SP.UI.ModalDialog.showModalDialog(options);
}

Sharepoint Create List or Library via c#

i have created Picture Library with some columns in List feature receiver event, and called below in feature activated event
we may create document, picture, video etc different types of library with the below code by simply changing in SPListTemplateType.YourDesiredLibrary

public void CreatePictureLibrary(SPWeb web)
{
try
{
if (web.Lists.TryGetList(Lists.PictureGallery.ListName) == null)
{
web.Lists.Add(Lists.PictureGallery.ListName, Lists.PictureGallery.ListDescription, SPListTemplateType.PictureLibrary);
web.Lists[Lists.PictureGallery.ListName].OnQuickLaunch = true;
}
SPList PictureGallery = web.Lists.TryGetList(Lists.PictureGallery.ListName);

if (PictureGallery != null)
{
web.AllowUnsafeUpdates = true;
if (PictureGallery.Fields.ContainsField(Lists.PictureGallery.Category) == false)
{
PictureGallery.Fields.Add(Lists.PictureGallery.Category, SPFieldType.Choice, false);
PictureGallery.Update();
SPFieldChoice objChoiceCol = (SPFieldChoice)PictureGallery.Fields[Lists.PictureGallery.Category];
objChoiceCol.Choices.Add(“Nature”);
objChoiceCol.Choices.Add(“Bike Pictures”);
objChoiceCol.Choices.Add(“Pictures”);
objChoiceCol.Choices.Add(“Events”);
objChoiceCol.Choices.Add(“Virtual Tour”);
objChoiceCol.Choices.Add(“Logo”);
objChoiceCol.EditFormat = SPChoiceFormatType.Dropdown;
objChoiceCol.Update();

PictureGallery.Update();

AddFieldInDefaultView(PictureGallery, Lists.PictureGallery.Category);
}

if (PictureGallery.Fields.ContainsField(Lists.PictureGallery.IsPublic) == false)
{
PictureGallery.Fields.Add(Lists.PictureGallery.IsPublic, SPFieldType.Boolean, false);
PictureGallery.Update();

AddFieldInDefaultView(PictureGallery, Lists.PictureGallery.IsPublic);
}

if (PictureGallery.Fields.ContainsField(Lists.PictureGallery.Event) == false)
{
PictureGallery.Fields.Add(Lists.PictureGallery.Event, SPFieldType.Text, false);
PictureGallery.Update();

AddFieldInDefaultView(PictureGallery, Lists.PictureGallery.Event);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
===============

private void AddFieldInDefaultView(SPList list, string FieldName)
{
SPView view = list.DefaultView;
view.ViewFields.Add(FieldName);
view.Update();
}

Sharepoint Create Lookup column via C#

i have created Lookup column via c# in ListsFeatureReceiver.cs class.

calling in FeatureActivated event


private void AddLookupColumnInEvent(SPWeb web)
{
try
{
SPList list = web.Lists.TryGetList(Lists.Event.ListName);
if (list != null)
{
web.AllowUnsafeUpdates = true;

SPContentType contentType = list.ContentTypes["Event"];
if (contentType != null)
{
if (contentType.FieldLinks[Lists.Event.Public] as SPFieldLink == null)
{
SPList parentList = web.Lists.TryGetList("ParentListName");
if (parentList != null)
{
list.Fields.AddLookup(Lists.Event.Public, parentList.ID, false);

SPFieldLookup lookup = list.Fields[Lists.Event.Public] as SPFieldLookup;

lookup.AllowMultipleValues = true;
lookup.LookupField = parentList.Fields[SPBuiltInFieldId.Title].InternalName;
lookup.Update();

//add in contenttypes
foreach (SPContentType contenttype in list.ContentTypes)
{
contenttype.FieldLinks.Add(new SPFieldLink(lookup));
contenttype.Update();

}
//add in views
foreach (SPView view in list.Views)
{
view.ViewFields.Add(lookup);
view.Update();
}

list.Update();
}
}
}

web.AllowUnsafeUpdates = false;
}
}
catch (Exception ex)
{
}
}