using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(file.Url, PersonalizationScope.Shared))
{
ContentEditorWebPart wp = new ContentEditorWebPart();
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("Root");
xmlElement.InnerText = "Test CEWP Contents";
wp.Content = xmlElement;
wp.Content.InnerText = xmlElement.InnerText;
wp.Title = "Test";
webPartManager.AddWebPart(wp, "Zone 1", 10);
}
Thursday, March 22, 2012
Wednesday, March 21, 2012
Programmatically create Sharepoint Pages
Props to Laura: http://www.sharepoint911.com/blogs/laura/Lists/Posts/Post.aspx?List=daba3a3b%2Dc338%2D41d8%2Dbf52%2Dcd897d000cf3&ID=67&Web=dbb90e85%2Db54c%2D49f4%2D8e97%2D6d8258116ca0
Open your site in SharePoint Designer, and click File –> New
–> ASPX
Click the Format menu, choose Master Page,
and click Attach Master Page
Click OK for the Default Master Page.
Click the PlaceHolderMain section in the middle of the page, click the
little chevron (the little gray button with an arrow on it), and click
Create Custom Content.
Put your cursor inside of the PlaceHolderMain section. On the
Insert Menu, choose SharePoint Controls, and
click Web Part Zone
View Source, copy/paste into the Resource.
string siteUrl = "http://localhost";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb("/"))
{
SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists.TryGetList("Site Pages");
string filename = string.Format("test{0}.aspx", DateTime.Now.ToString("yyyymmddhhmmss"));
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] page = encoding.GetBytes(Resource1.ASPXTemplate);
SPFile file = lib.RootFolder.Files.Add(filename, page, true);
}
}
Open your site in SharePoint Designer, and click File –> New
–> ASPX
Click the Format menu, choose Master Page,
and click Attach Master Page
Click OK for the Default Master Page.
Click the PlaceHolderMain section in the middle of the page, click the
little chevron (the little gray button with an arrow on it), and click
Create Custom Content.
Put your cursor inside of the PlaceHolderMain section. On the
Insert Menu, choose SharePoint Controls, and
click Web Part Zone
View Source, copy/paste into the Resource.
string siteUrl = "http://localhost";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb("/"))
{
SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists.TryGetList("Site Pages");
string filename = string.Format("test{0}.aspx", DateTime.Now.ToString("yyyymmddhhmmss"));
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] page = encoding.GetBytes(Resource1.ASPXTemplate);
SPFile file = lib.RootFolder.Files.Add(filename, page, true);
}
}
CreateField with Default Value
public static void CreateFieldWithDefault(SPList list, string fieldName, string defaultValue, SPFieldType spFieldType)
{
try
{
string fieldNameNoSpaces = TrimSpaces(fieldName);
string displayName = list.Fields.Add(fieldNameNoSpaces, spFieldType, false);
SPField field = list.Fields[fieldNameNoSpaces];
field.Title = fieldName;
if (spFieldType != SPFieldType.Boolean)
{
field.DefaultValue = defaultValue;
}
field.Update();
if (spFieldType == SPFieldType.DateTime)
{
SPFieldDateTime dt = (SPFieldDateTime)field;
dt.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
dt.Update();
}
SPView view = list.DefaultView;
view.ViewFields.Add(fieldName);
view.Update();
}
catch (Exception ex)
{
Logging.LogException("CreateFieldWithDefault", ex);
}
}
private static string TrimSpaces(string fieldName)
{
string[] names = fieldName.Split(' ');
string fieldNameNoSpaces = string.Empty;
foreach (string name in names)
{
fieldNameNoSpaces += name;
}
return fieldNameNoSpaces;
}
{
try
{
string fieldNameNoSpaces = TrimSpaces(fieldName);
string displayName = list.Fields.Add(fieldNameNoSpaces, spFieldType, false);
SPField field = list.Fields[fieldNameNoSpaces];
field.Title = fieldName;
if (spFieldType != SPFieldType.Boolean)
{
field.DefaultValue = defaultValue;
}
field.Update();
if (spFieldType == SPFieldType.DateTime)
{
SPFieldDateTime dt = (SPFieldDateTime)field;
dt.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
dt.Update();
}
SPView view = list.DefaultView;
view.ViewFields.Add(fieldName);
view.Update();
}
catch (Exception ex)
{
Logging.LogException("CreateFieldWithDefault", ex);
}
}
private static string TrimSpaces(string fieldName)
{
string[] names = fieldName.Split(' ');
string fieldNameNoSpaces = string.Empty;
foreach (string name in names)
{
fieldNameNoSpaces += name;
}
return fieldNameNoSpaces;
}
SPItemEventReceiver - SPItem SPField updated
public override void ItemAdded(SPItemEventProperties properties)
{
// Check that ths is the list we care about
if (false == string.Equals(properties.ListTitle, "ListName", StringComparison.InvariantCultureIgnoreCase))
{
return;
}
// Check if this is the Field name that we care about
if (null == properties.AfterProperties["FieldName"])
{
return;
}
{
// Check that ths is the list we care about
if (false == string.Equals(properties.ListTitle, "ListName", StringComparison.InvariantCultureIgnoreCase))
{
return;
}
// Check if this is the Field name that we care about
if (null == properties.AfterProperties["FieldName"])
{
return;
}
// If you get here, field was added/updated.
}
private static void SnapshotToXLS(SPList list, SPList docLib)
{
try
{
SPListItemCollection listItems = list.GetItems(list.Views["View"]);
DataTable items = listItems.GetDataTable();
string filename = string.Format("{0}.xml", DateTime.Now.ToString("yyyymmddhhmmss"));
MemoryStream contents = new MemoryStream();
items.WriteXml(contents);
SPFile file = docLib.RootFolder.Files.Add(filename, contents);
}
{
try
{
SPListItemCollection listItems = list.GetItems(list.Views["View"]);
DataTable items = listItems.GetDataTable();
string filename = string.Format("{0}.xml", DateTime.Now.ToString("yyyymmddhhmmss"));
MemoryStream contents = new MemoryStream();
items.WriteXml(contents);
SPFile file = docLib.RootFolder.Files.Add(filename, contents);
}
Monday, March 19, 2012
Sharepoint Menu Manipulation Headers versus Links
Everything gets created as a link, if you want just a header, use the following code:
SPNavigationNode header = new SPNavigationNode("Header", "");
header = nodes.AddAsLast(header);
header.Properties["NodeType"] = "Heading";
header.Properties["BlankUrl"] = "true";
header.Update();
SPNavigationNode header = new SPNavigationNode("Header", "");
header = nodes.AddAsLast(header);
header.Properties["NodeType"] = "Heading";
header.Properties["BlankUrl"] = "true";
header.Update();
Monday, March 5, 2012
Delete all items from a Sharepoint list
private static void EmptyList(SPSite site, SPList list)
{
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("");
string command = "" + list.ID + " {0} Delete ";
foreach (SPListItem item in list.Items)
{
sbDelete.Append(string.Format(command, item.ID.ToString()));
}
sbDelete.Append(" ");
site.RootWeb.ProcessBatchData(sbDelete.ToString());
}
{
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("
string command = "
foreach (SPListItem item in list.Items)
{
sbDelete.Append(string.Format(command, item.ID.ToString()));
}
sbDelete.Append("
site.RootWeb.ProcessBatchData(sbDelete.ToString());
}
Subscribe to:
Posts (Atom)