Tuesday, May 29, 2012

OpenXML doesn't work with Office 365(at least now)

When you work with OpenXmL and SharePoint 365, you have to work by Sandboxed Solution.
And that what you receive :

Error validating assembly 'DocumentFormat.OpenXml.dll'

I found this post which didn't me happy.

Monday, May 28, 2012

jQuery - Extension to SPServices.SPCascadeDropdowns

We use SPServices.SPCascadeDropdowns , after 20 items it looks like this:




Instead of:









but the problem is that it doesn't work with Wiki Pages.
Therefore I wrote little extension which helps me to solve this problem.

I changed two things:
1.
After : "var defaultViewUrl = $(this).attr("DefaultViewUrl");"
insert code :

if (listPath.indexOf('/PAGES/') > 0)
{ listPath = listPath + 'FORMS/'; }



2.
Instead of "$(columnSelect.Obj).closest("td").prepend(simpleSelect);"
replace with :

if ($(columnSelect.Obj).closest("td").length)
{ $(columnSelect.Obj).closest("td").prepend(simpleSelect); }
else
{ $(columnSelect.Obj).closest("span").prepend(simpleSelect); }


Tuesday, May 22, 2012

Set user permissions to SPList

Sometimes I need to set permissions for special SPUser to special SPList.
Therefore , I developed winform solution, which helps me.

First of all, I use BackgroundWorker just to execute the operation on a separated thread.
Second, I aggregate fields from the form, which help me to bring a relevant data:
SiteURL - www.contoso.com
List - myList
ItemField - SPField("Display Name") For example item["ModifiedBy"]
ItemField Text - For example item["ModifiedBy"] = "Victor"
User - I use SPWeb.EnsureUser






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace GivenPermissions
{
public partial class Form1 : Form
{

BackgroundWorker bg2 = new BackgroundWorker();

public Form1()
{
InitializeComponent();
}

private void btnRun_Click(object sender, EventArgs e)
{
bg2.DoWork += new DoWorkEventHandler(bg2_DoWork);
bg2.ProgressChanged += new ProgressChangedEventHandler(bg2_ProgressChanged);
bg2.RunWorkerAsync();
}

private void bg2_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{

}




void bg2_DoWork(object sender, DoWorkEventArgs e)
{
try
{
using (SPSite site = new SPSite(txtSiteUrl.Text))
{
using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists[txtList.Text];

SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";


SPListItemCollection col = list.GetItems(query);

foreach (SPListItem item in col)
{
if (item[txtItemField.Text] != null && string.Compare(item[txtItemField.Text].ToString(), txtItemFielText.Text) == 0)
{


SPUser user = web.EnsureUser(txtUser.Text);
SPRoleType roleType = SPRoleType.Contributor;


SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)user);
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);

roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);

item.Update();

}
}


}
}




if (txtMessage.InvokeRequired)
{
txtMessage.Invoke(new MethodInvoker(delegate
{


txtMessage.AppendText("DONE ! ! !");



}));
}
}
catch (Exception ex)
{
if (txtMessage.InvokeRequired)
{
txtMessage.Invoke(new MethodInvoker(delegate
{

txtMessage.AppendText(ex.Message + "\n" + ex.StackTrace);

}));
}
}



}
}
}


Monday, May 21, 2012

The Web application at http://contoso.com could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

This is the message you can receive, when you try to run your application(winform or console) on SharePoint Application.
The reason is that a application runs on x86.
Here are a few steps which will help you (I hope):
1.Right-Click on the application -> Properties:


2. Build -> Platform target :



3. Change it on "Any CPU" and "Save":



Thursday, May 10, 2012

JavaScript - Get the URL without the filename.

When I need to get  URL without  the file's name , for example:

instead of

http://contoso.com/pages/test.aspx

I need

http://contoso.com/pages/

I use this script (with regex pattern  "/^(http.+\/)[^\/]+$/"):

location.href.match( /^(http.+\/)[^\/]+$/ )[1]

Wednesday, May 9, 2012

JavaScript - Get file name from URL

This function get file name from URL by javascript:

function getFileName()
 {

      var url = document.location.href;

      url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));

      url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));

      url = url.substring(url.lastIndexOf("/") + 1, url.length);

     return url;
}

Thursday, May 3, 2012

T-SQL Script - Reduce log file of database

Sometimes I need to reduce log files of SQL Databases
This is the script which helps me:


USE DataBase_Name;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE DataBase_Name
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE ( DataBase_Name _log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE DataBase_Name
SET RECOVERY FULL;
GO