Thursday, 20 December 2012

Programmatically add lines of text to a Rich Text Box field on an InfoPath form using C# code

Following the Code:-

XPathNavigator nav = MainDataSource.CreateNavigator();
XPathNavigator rtfNav = nav.SelectSingleNode("//my:rtfField", NamespaceManager);
XmlDocument doc = new XmlDocument();
XmlElement elm = doc.CreateElement("div", "http://www.w3.org/1999/xhtml");
elm.InnerText = "Line 1";
doc.AppendChild(elm);
rtfNav.AppendChild(doc.DocumentElement.CreateNavigator());
doc = new XmlDocument();
elm = doc.CreateElement("div", "http://www.w3.org/1999/xhtml");
elm.InnerText = "Line 2";
doc.AppendChild(elm);
rtfNav.AppendChild(doc.DocumentElement.CreateNavigator());

How to get HTML tags to appear as HTML and not as plain text in a Rich Text Box

Following the code:-


MainDataSource.CreateNavigator().SelectSingleNode("//my:rtfField", NamespaceManager).AppendChild("<h2 xmlns=\"http://www.w3.org/1999/xhtml\">Header Text</h2><p xmlns=\"http://www.w3.org/1999/xhtml\">This is some paragraph text.</p>");

How do I call a web service through code?

Following the code:_

// Create an XPathNavigator object to navigate the data source of the web service
XPathNavigator nav = DataSources["HelloWorld"].CreateNavigator();

// Set the value of the parameter to pass to the web service
nav.SelectSingleNode("//dfs:queryFields/tns:HelloWorld/tns:name", NamespaceManager).SetValue("myValue");

// Call the web service
DataSources["HelloWorld"].QueryConnection.Execute();

// Retrieve the results returned by the web service
string results = nav.SelectSingleNode("//dfs:dataFields/tns:HelloWorldResponse/tns:HelloWorldResult", NamespaceManager).Value;

How do I set the value of an InfoPath field through code?

Following the code:-

XPathNavigator nav = MainDataSource.CreateNavigator();
nav.SelectSingleNode("//my:field1", NamespaceManager).SetValue("myValue");

How do I retrieve the value of an InfoPath field through code?

Following the code:-

XPathNavigator nav = MainDataSource.CreateNavigator();
string fieldValue = nav.SelectSingleNode("//my:field1", NamespaceManager).Value;

Friday, 30 November 2012

Using SharePoint PowerShell, Empty the Recycle Bin.


$w = get-spweb http://server-name/sites/site-name
$w.RecycleBin.DeleteAll()

Using SharePoint PowerShell, Delete Multiple Items form SharePoint 2010 List or Library.

Batch delete multiple items using power shell in sharepoint 2010 list or library.

- You provide only server-name and list or library -name.
- Items delete from list or library.
- Also Items delete from Recycle Bin.   

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
write-host

$siteUrl = "http://Server-name/sites/site-name"
$listName = "List/Library-name"
$batchSize = 1000
write-host "Opening web at $siteUrl..."
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb()
write-host "Web is: $($web.Title)"
$list = $web.Lists[$listName];
write-host "List is: $($list.Title)"
while ($list.ItemCount -gt 0)
{
  write-host "Item count: $($list.ItemCount)"
  $batch = "<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"
  $i = 0
  foreach ($item in $list.Items)
  {
    $i++
    write-host "`rProcessing ID: $($item.ID) ($i of $batchSize)" -nonewline
    $batch += "<Method><SetList Scope=`"Request`">$($list.ID)</SetList><SetVar Name=`"ID`">$($item.ID)</SetVar><SetVar Name=`"Cmd`">Delete</SetVar><SetVar Name=`"owsfileref`">$($item.File.ServerRelativeUrl)</SetVar></Method>"
    if ($i -ge $batchSize) { break }
  }
  $batch += "</Batch>"
  write-host
  write-host "Sending batch..."
  # We execute it
  $result = $web.ProcessBatchData($batch)
  write-host "Emptying Recycle Bin..."
  # We remove items from recyclebin
  $web.RecycleBin.DeleteAll()
  write-host
  $list.Update()
}
write-host "Done."