Etykiety

Pokazywanie postów oznaczonych etykietą CodeSnippet. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą CodeSnippet. Pokaż wszystkie posty

niedziela, 21 czerwca 2015

Create SendTo connection programmatically

private const string CONTENT_ORGANIZER_URL = "/_vti_bin/officialfile.asmx";

/// To run this code without Access Denied error, RemoteAdministratorAccessDenied must be set False. Do it with PowerShell:
/// $contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
/// $contentService.RemoteAdministratorAccessDenied = $false
/// $contentService.Update()
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webapp = newRC.WebApplication;
       var newhost = new SPOfficialFileHost(true);
       newhost.OfficialFileUrl = new Uri("url of site with content organizer feature on" + CONTENT_ORGANIZER_URL);
       newhost.OfficialFileName = "Your connection name";
       newhost.Explanation = "Your connection description";
       newhost.ShowOnSendToMenu = false;
       newhost.Action = SPOfficialFileAction.Move;
webapp.OfficialFileHosts.Add(newhost);
webapp.Update();
});


Code snippet adding javascript that maximizes dialog window

if (SPContext.Current.IsPopUI)
{
   if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(), "ScriptWinMax"))
   {
      string script = @"function _maximizeWindow() {
         var currentDialog = SP.UI.ModalDialog.get_childDialog();
         if (currentDialog != null && !currentDialog.$S_0)
            currentDialog.$z();
         }       
         ExecuteOrDelayUntilScriptLoaded(_maximizeWindow, 'sp.ui.dialog.js');";

      Page.ClientScript.RegisterClientScriptBlock(GetType(), "ScriptWinMax", script, true);
   }
}

jQuery 'Hello world' snippet


<script type="text/javascript">
   $(document).ready(function () {
      // simple test jquery is here
      $("#hello").html("Hello World by JQuery");
      });
   });
</script>

<div id="hello"></div>

Code sample of using ListViewWebPart

Application Page:

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
   <asp:UpdatePanel ID="panelLV" runat="server" UpdateMode="Conditional"></asp:UpdatePanel>
</asp:Content>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   { 
      createLVWP();
   }
}

private void createLVWP()
{
   SPWeb web = SPContext.Current.Web;
   SPList list = web.GetList(
         SPUrlUtility.CombineUrl(web.ServerRelativeUrl, myListUrl));

   lvwp = new ListViewWebPart();
   lvwp.ListName = list.ID.ToString("B").ToUpper();
   lvwp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
   lvwp.ChromeType = PartChromeType.None;
   
   filterLVWP();
   this.panelLV.ContentTemplateContainer.Controls.Add(lvwp);
}

private void filterLVWP()
{
   string query = string.Format(
@"<Contains>
<FieldRef Name='myFieldName' /><Value Type='Text'>{0}</Value>
</Contains>", myFieldValue);

   XmlDocument doc = new XmlDocument();
   doc.LoadXml(lvwp.ListViewXml);
   XmlNode queryNode = doc.SelectSingleNode("//Query");
   XmlNode whereNode = queryNode.SelectSingleNode("Where");
   if (whereNode != null)
      queryNode.RemoveChild(whereNode);

   XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "Where", String.Empty);
   newNode.InnerXml = query.ToString();
   queryNode.AppendChild(newNode);
   lvwp.ListViewXml = doc.OuterXml;
}

wtorek, 12 listopada 2013

Add and deploy solution with PowerShell

Add-SPSolution -LiteralPath "<path>\MySolution.wsp" | Out-Null
echo " > Adding MySolution..."
start-sleep -s 10
$sln = get-spsolution -identity MySolution.wsp
Install-SPSolution -Identity $sln -GACDeployment
while($sln.Deployed -eq $false) {
echo " > Deploying in progress..."
start-sleep -s 10
}
echo "MySolution is deployed."
Enable-SPFeature -Identity "MyFeatureName" -Url "<my site url>"

Need to know what exactly is your feature name? This command will display all web-scoped features:

Get-SPFeature | Where-Object { $_.Scope -eq "Web" } | Format-Table -Property DisplayName -AutoSize

Retrack solution with PowerShell

Set-ExecutionPolicy unrestricted
echo " "
echo "MySharepointSolution"
$sln = get-spsolution -identity MySharepointSolution.wsp
uninstall-spsolution -identity MySharepointSolution.wsp -confirm:$false
echo "Started solution retraction..." 
while($sln.JobExists) {
echo " > Working..."
start-sleep -s 10 
}
remove-spsolution -identity MySharepointSolution.wsp -confirm:$false
echo "Removed MySharepointSolution"
echo " "

wtorek, 22 października 2013

Replace columns in DataTable and by the way insert at specified position (code snippet)



DataTable table = GetSomeDataTable();

table.Columns.Add("NewNumber", typeof(int)).SetOrdinal(table.Columns["OldColumn1"].Ordinal);
table.Columns.Add("NewText", typeof(string)).SetOrdinal(table.Columns["OldColumn2"].Ordinal);

foreach (DataRow row in table.Rows)
{
  row["NewNumber"] = Transform1(row["OldColumn1"]);
  row["NewText"] = Transform2(row["OldColumn2"]);
}

table.Columns.Remove("OldColumn1");
table.Columns.Remove("OldColumn2");