RSS

Category Archives: Code Snippets

Resolved: Could not load file or assembly ‘Microsoft.SharePoint.Sandbox, Version=12.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXX’ or one of its dependencies…


I have a remote sharepoint solution that contains SharePoint dll’s reference. Whenever I run the solution, start page get crash and show me this error.

“Could not load file or assembly ‘Microsoft.SharePoint.Sandbox, Version=12.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXX’ or one of its dependencies. An attempt was made to load a program with an incorrect format.”

Well every time I run the project it creates Microsoft.SharePoint.Sandbox.dll and Microsoft.SharePoint.Sandbox.xml inside the bin folder and it creates the problem. Well there are lots many things have written on web regarding this problem. I follow all these things but could not get rid of this error. Finally I decided to fix this problem according to my way. So below are the steps to fix this problem permanently.

1. Inside solution explorer (Visual Studio), select the project and right click on that.

2. From context menu, click on properties.

3. When properties page opens, Click on Build Events.

4. Under Post – build event command line, copy and paste below lines

cd $(TargetDir)

del Microsoft.SharePoint.Sandbox.dll

del Microsoft.SharePoint.Sandbox.xml

Done!

NOTE: please replace with your dll name.

Advertisements
 
 

Programatically select and bind Sharepoint list items to ASP.NET controls


  • Open Visual Studio 2010
  • Create New Project
  • Select your Sharepoint site
  • Create Visual Webpart
  • Add LAbels,Textboxes or Dropdownlist (NOTE: Update control names in your code accordingly)
  • Use below code in page load event

try
{

SPQuery query = new SPQuery();
query.Query = “”;
query.ViewFields = “”;
query.RowLimit = 100;

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{

using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists.TryGetList(“—ADD LIST NAME—“);

//Check List Exists

if (list != null)

{

//Check ITEMS Exists

if (list.GetItems(query).GetDataTable() != null)
{
DataTableReader rdr = list.GetItems(query).GetDataTable().CreateDataReader();

if (rdr.Read())

//Bind list items to ASP.NET Label

Label1.Text = rdr[“Test_x0020_Item1”].ToString();
Label2.Text = rdr[“TestField2”].ToString();

//To bind DROPDOWNLIST uncomment below code
// dropdownlist1.DataSource = rdr;
// dropdownlist1.DataTextField = “Title”;
// dropdownlist1.DataValueField = “Title”;
// dropdownlist1.DataBind();

//To bind REPEATER uncomment below code
// Repeater1.DataSource = rdr;
// Repeater1.DataBind();

rdr.Close();

}
else
{
lblInfo.Text = “No Items Found”;
}
}
else
{
lblErrorMessage.Text = “List not found.Please make sure list is created and go to webpart settings to add the List name “;
}
}

}
}

catch (Exception ex)
{
lblErrorMessage.Text = ex.Message.ToString();
}

 
 

Tags: ,

Programatically Insert Items to Sharepoint 2010 List using C#.net


  • Open Visual Studio 2010
  • Create New Project
  • Select Your Sharepoint site
  • Create New Visual Webpart
  • Add Textbox,Label and Button as required(NOTE: Controls names You need to update in below code)
  • Add below code in button click event the code

try
{

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{

SPList List = web.Lists.TryGetList(“—ADD LIST NAME—“);
if (List != null)
{

SPListItem NewItem = List.Items.Add();

{
web.AllowUnsafeUpdates = true;

NewItem[“Title”] = Label1.Text;
NewItem[“Employee Name”] = Label2.Text;
NewItem[“Comments”] = Textbox1.Text;

NewItem.Update();

//int newID = NewItem.ID;

web.AllowUnsafeUpdates = false;

lblInfo.Text = “Item Inserted Successfully.”;

}
}
}

}

}
else
{
lblErrorMessage.Text = “List not found. Configure the list from webpart settings”;
}
}

}
}

catch (Exception ex)
{
lblErrorMessage.Text = ex.Message.ToString();
}

 
2 Comments

Posted by on November 14, 2012 in Code Snippets, Uncategorized, Visual web part

 

Tags: , ,

Access List data using LINQ in SharePoint 2010


Here is a  great Article  by Franck Rice from Microsoft to develop and deploy a visual Web Part that reads data from two lists by using a LINQ query and displays it in a DataGrid control.

Click to see the Article

To Know more about LINQ

 
1 Comment

Posted by on April 12, 2011 in Code Snippets, LINQ, Sharepoint Server 2010

 

Tags: ,

Programmatically insert an item in sharepoint list using ASP.NET user control


  • Create a new web project in Visual studio
  • Add reference to Microsoft.SharePoint.dll  (you can find from C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI)
  • Create an new user control(.ascx)
  • Import below namespaces
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Search;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
  • Insert Textboxes, buttons….whatever u required
  • On button click paste the code below
  • To host the user control in sharepoint use smartpart from codeplex.

CODE SNIPPET

{

            try
            {

               
                //create the object of SPSite
             SPSite oSiteCollection = SPContext.Current.Site;
           

           // SPSite oSiteCollection = new SPSite(“http://mysite:3434“);

                //create the object of SPWeb with the help of SPSite object:
                SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
                oWebsiteRoot.AllowUnsafeUpdates = true;

                //create the connection to the custom List and create object of SPListItemCollection
                SPList oList = oWebsiteRoot.Lists[“MyListName”];
                SPListItemCollection listItemCOll;

                //add the item in the User List
                SPListItem oListItem = oList.Items.Add();
                oListItem[“Name”] = txtName.Text;
                oListItem[“Email”] = txtEmail.Text;
                oListItem[“Color”] = ddldropdown.SelectedItem;
                oListItem[“AssignTo”] = txtAssignto.Text;

                oListItem.Update();             
                            
             
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }

View the UPDATED CODE HERE

 
16 Comments

Posted by on April 21, 2010 in ASP.NET, Code Snippets

 

Tags: ,

How to add numbering column in dataview webpart?


  • Open the page in sharepoint designer
  • Insert dataview wepart
  • Select your columns to query
  • Now click on the first column & insert new column on left
  • In the new column just paste below code…thts it

<xsl:value-of select=”position()” />

If you need any bulleted images. insert an image in the column.

 
1 Comment

Posted by on April 21, 2010 in Code Snippets, Sharepoint Designer

 

SharePoint Data View Conditional Formatting based on user permissions (IfHasRights)


Below are the values for conditional formatting

example usage : <xsl:if test=”ddwrt:IfHasRights(“REPLACE THIS WITH BELOW VALUE”)”><img alt=”” border=”0″ src=”../../PublishingImages/btn_AddActionItem.jpg” width=”191″ height=”37″ /></xsl:if>

ViewListItems – 1

AddListItems – 2

EditListItems – 4

DeleteListItems – 8

ApproveItems – 16

OpenItems – 32

ViewVersions – 64

DeleteVersions – 128

CancelCheckout – 256

PersonalViews – 512

ManageLists – 2048

ViewFormPages – 4096

Open – 65536

ViewPages – 131072

AddAndCustomizePages – 262144

ApplyThemeAndBorder – 524288

ApplyStyleSheets – 1048576

ViewUsageData – 2097152

CreateSSCSite – 4194314

ManageSubwebs – 8388608

CreateGroups – 16777216

ManagePermissions – 33554432

BrowseDirectories – 67108864

BrowseUserInfo – 134217728

AddDelPrivateWebParts – 268435456

UpdatePersonalWebParts – 536870912

ManageWeb – 1073741824

UseRemoteAPIs – 137438953472

ManageAlerts – 274877906944

CreateAlerts – 549755813888

EditMyUserInfo – 1099511627776

EnumeratePermissions – 4611686018427387904

FullMask – 9223372036854775807

To ensure that users only see the edit button if they are the author of the posting, or a site owner we added the following conditional formatting:

test=”ddwrt:IfHasRights(2048) or contains(@Author,$Userid)”

 
4 Comments

Posted by on October 19, 2009 in Code Snippets, Sharepoint Designer

 

Tags: