Thursday, August 27, 2009

Create SharePoint Document Library programmatically using C#

We can create our own custom document library programmatically, here is the code shown below

SPSite site = SPContext.Current.Site as SPSite;
SPWeb web = site.RootWeb as SPWeb;
try
{ //Create a new library as the permanent library
Guid customListID = web.Lists.Add("CUSTOM DOCUMENT LIBRARY", "CUSTOM DOCUMENT LIBRARY", SPListTemplateType.DocumentLibrary);
web.Update();
}

Thursday, August 20, 2009

SharePoint Cannot complete this action error

When i am trying to count the CheckedOut items from SharePoint Document Library using c# code in visual studio as shown below

SPSite mysite = new SPSite("http://servername:portnumber");
SPWeb myWeb = mysite.OpenWeb();
myWeb.AllowUnsafeUpdates = true;
SPList Document = myWeb.Lists["Shared Documents"];
SPQuery query = new SPQuery();
query.Query ="<Where><IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull></Where>";
SPListItemCollection checkedOutItems= Document.GetItems(query);
Response.Write(checkedOutItems.Count);

I got the following error:
System.Runtime.InteropServices.COMException: Cannot complete this action

Resolution:

We need add the tag "<identity impersonate='true'/>" tag in the web.config file of the project in the visual studio.
This is because the ASP.NET doesnt allows you to read the outside documents i.e the documents(folders) are not in ASP.NET Application.So we need to keep impersonate = true in web.config file.

Hopes this helps you....

Monday, August 17, 2009

c# programmatically check the "<appSettings>" tag in web.config file of sharepoint sites

To check programmatically weather the appSettings tag is present in the web.config file of SharePoint sites, here is the code

using (SPSite site = new SPSite("http://servername:portnumber"))
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", mySite.WebApplication.Name);
AppSettingsSection appSettings = config.AppSettings;
bool isAppSettingsTagPresent = appSettings.ElementInformation.IsPresent;
}

hope this helps you very much...

Friday, August 7, 2009

Programmatically get the sharePoint List Url in C#

To get the Share Point list "url" based on list "Guid", here is the code shown below

SPSite Site = SPContext.Current.Site;
SPWeb web = Site.OpenWeb();
Guid listid = new Guid("{xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
web.AllowUnsafeUpdates = true;
SPList List = web.Lists[listid];
string url = SPContext.Current.Site.Url + "/" + List.RootFolder.Url;
Response.Write(url);
web.AllUnSafeUpdates = false;
web.Dispose();

Hopes this helps you a lot.

Thursday, August 6, 2009

Set "everyone" Permission to Folder using C# in Asp.Net

Here is the simple C# code to set "everyone" permissions to the folder .

string redirectionFolder = @"D:\test\subtest\";

FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl,AccessControlType.Allow);
DirectorySecurity dirSecurity = new DirectorySecurity(redirectionFolder, AccessControlSections.Group);
dirSecurity.AddAccessRule(everyOne);
Directory.SetAccessControl(redirectionFolder, dirSecurity);

Note:
If you get the error "Attempted to perform an unauthorized operation"

add the tag (<identity impersonate="true"/>) in your web.config file.

Steps to show the RSS Feed Data using SharePoint Designer

It is very simple to get data from RSS feeds using SP Designer.
I will show with a simple example.
Please follow the steps given below - (Explained with example)

1)Open the SP Designer, and open the site and the page where you want to show the RSS Feed data.
2)I am taking the RSS Feed Url of my blog (http://rajkamal29.blogspot.com/atom.xml).
3)In the right side of the SP Designer you will find the "Data Source Library" Toolbar,Go to "Server-side Scripts" Options and click "Connect to script or RSS Feed".

3)Now you will get an "Data Source Properties" window, thenn select "HTTP Get" method in the drop down and data command "Select", finally provide the RSS Feed Url of my blog as show above (http://rajkamal29.blogspot.com/atom.xml) in the "Url to a Server-side script" Section and click Ok.

4)Then select "Show Data" which is located when you click on the right arrow of the RSS feed connection as shown below.

5) Then you will go to the "Data Source Details" toolbar, where you will find all the Fields.
6) Select the "Fields" you want to display and click on the "Insert Selected Fields as.." button located on the top of the fields and click "Single Item View" tab.

7)You will find the data in the page( in Sharepoint Designer) as "Data form" webpart,then customize data form web part according your requirement as shown below.

Wednesday, August 5, 2009

Steps to remove Projects from Recent Projects in Visual Studio Start Page

To remove the Projects from Recent Projects in Visual Studio, there is no direct option to do this.We need to go to Registry Editor and Remove the Projects,follow the steps as shown below.
1) Go to Run in the start menu, type regedit
.
2) Go to HKEY_CURRENT_USER --> Softwares --> Microsoft --> VisualStudio --> 8.0 (if we want remove the recent projects in VS2005) or 9.0 (for vs2008) --> ProjectMRUList
3) Select and delete the Projects, which we don't want to show in the Recent Projects List of VS2005 or VS2008.

Next time when you start Visual Studio, the removed project through registry editor will not be listed on the start page.

Monday, August 3, 2009

C# Code to Update Document in SharePoint Document library

SPSite mySite =null;
SPWeb myWeb =null;

/* represents the current site */

using (mySite = SPContext.Current.Site)
{
using (myWeb = mySite.OpenWeb())
{

/* Modifes Windows SharePoint Services data in some way, you may need to allow unsafe updates on the Web site, without requiring a security validation. */

myWeb.AllowUnsafeUpdates = true;
Guid listId = new Guid(listID);
SPList documentLibrary = myWeb.Lists[listId];
SPListItem document = documentLibrary.GetItemById(itemID);

SPFolder oFolder = documentLibrary.RootFolder;

SPFileCollection collFiles = oFolder.Files;
SPFile oFile = null;

/* overwrite the file if exists in the document library */

oFile = collFiles.Add(document.Url, bytes, true);

SPListItem listItem = oFile.Item;

listItem["Col1"] = Value1;
listItem["Col2"] = Value2;

/* updates the versionsing of the document library also*/
listItem.UpdateOverwriteVersion();

myWeb.AllowUnsafeUpdates = false;
myWeb.Dispose();

}
}