Hi,
I am building a visual webpart, and I have a class that gets the url's of various things such as forms, and permissions pages and library settings pages. I tried to implement them myself which seems to work. I just saw the link for an actual page, and then replaced its values with variables. But I am not sure if that's the right way to get the link. I want it to work with all cases.
This is what I got so far in my class:
Can anyone please advise if I am doing something not right, and show the right way of doing it?
Thanks
public class SharePoint
{
public SPContext context;
public SPWeb web;
public SPSite site;
public SPListCollection sp_lists;
public SPDocumentLibrary library;
public SPFieldCollection fields;
public String webURL = null;
public String siteURL = null;
public String pageURL = null;
public String pageURLWithoutQuery = null;
public String libraryName = null;
public String libraryURL = null;
public String librarySiteRelativeURL = null;
public String libraryNewFormURL = null;
public String libraryNewFolderURL = null;
public String librarySettingsURL = null;
public SharePoint()
{
context = SPContext.Current;
web = context.Web;
site = context.Site;
sp_lists = web.Lists;
webURL = web.Url;
siteURL = site.Url;
pageURL = webURL + System.Web.HttpContext.Current.Request.RawUrl;
pageURLWithoutQuery = (new Uri(pageURL)).GetLeftPart(UriPartial.Path);
}
public void configureValues()
{
libraryURL = siteURL + "/" + library.RootFolder.Url;
libraryName = Path.GetFileNameWithoutExtension(libraryURL);
librarySiteRelativeURL = siteURL + library.ParentWebUrl;
libraryNewFormURL = webURL + "/_layouts/Upload.aspx?List={" + library.ID + "}&RootFolder=/" + libraryName;
libraryNewFolderURL = libraryURL + "/Forms/Upload.aspx?RootFolder=/" + libraryName + "&Type=1&IsDlg=1";
librarySettingsURL = siteURL + "/_layouts/listedit.aspx?List={" + library.ID + "}";
}
public List<MySPListItem> GetItemsFromLibrary(string orderByColumnName, bool ordered, SPFolder folder, Report RO, WebpartSettings WPS)
{
SPQuery Query = new SPQuery();
if (folder != null) Query.Folder = folder;
Query.Query = string.Concat(
"<Where>","<Or>","<Eq><FieldRef Name='File_x0020_Type'/><Value Type='Text'>pdf</Value></Eq>","<Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Lookup\">1</Value></Eq>","</Or>","</Where>");
Query.ViewFields = string.Concat(
"<FieldRef Name='" + fields.GetField(orderByColumnName).InternalName + "'/>","<FieldRef Name='ID'/>","<FieldRef Name='Name'/>");
SPListItemCollection collListItems = library.GetItems(Query);
List<MySPListItem> myItems = ConvertToMyCollection(collListItems, RO, WPS);
if (ordered) myItems = MySPListItem.SortItems(myItems, orderByColumnName);
return myItems;
}
public List<MySPListItem> ConvertToMyCollection(SPListItemCollection itemCollection, Report RO, WebpartSettings WPS)
{
List<MySPListItem> myItems = new List<MySPListItem>();
foreach (SPListItem item in itemCollection)
{
myItems.Add(new MySPListItem(item, WPS.ORDER_BY_COLUMN));
}
return myItems;
}
public static void UpdateItemToSharepoint(SPListItem item, string column, object val, Report report_object)
{
try
{
item[column] = val; // update the items column value
item.Update(); // save changes to the item
}
catch (Microsoft.SharePoint.SPException)
{
report_object.Log("An error occurred when saving changes to the document. Maybe due to concurrent changes from another user. Please try again after a minute.");
}
catch (Exception ex)
{
report_object.Log("An error occured with " + item.Name + ":\n" + Report.GetErrorInfo(ex));
}
}
public static Object GetFolderOrderValue(SPFolder folder)
{
return folder.GetProperty("Order Folder By");
}
public static void SetFolderOrderValue(SPFolder folder, int value, Report report_object)
{
if (!folder.Exists)
{
report_object.Log("The folder '" + folder.Name + "' does not exist.");
return;
}
try
{
folder.SetProperty("Order Folder By", value);
folder.Update();
}
catch (Microsoft.SharePoint.SPException)
{
report_object.Log("An error occurred when saving changes to the folder. Maybe due to concurrent changes from another user. Please try again after a minute.");
}
catch (Exception ex)
{
report_object.Log("An error occured with " + folder.Name + ":\n" + Report.GetErrorInfo(ex));
}
}
public void AllowUpdatesToSharepoint(bool is_ok)
{
if (web.AllowUnsafeUpdates ^ is_ok)
{
web.AllowUnsafeUpdates = is_ok;
web.Update();
}
}
public bool ListExists(string sp_list)
{
return web.Lists.TryGetList(Path.GetFileName(sp_list)) != null;
}
/// <summary>
/// Returns the url to the editform of the specified item.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public static string GetEditformURLFromItem(SPListItem item)
{
const string format = "{0}?id={1}";
return string.Format(System.Globalization.CultureInfo.InvariantCulture, format, item.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].ServerRelativeUrl, item.ID.ToString());
}
/// <summary>
/// Returns the url to the document associated to the specified item.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public string GetFullURLOfItem(SPListItem item)
{
return webURL + "/" + item.Url;
}
public string GetHttpFolderLink(string folderName)
{
return (librarySiteRelativeURL + "/" + folderName).Replace("/", "\u002f").Replace(" ", "\u0020");
}
/// <summary>
/// Returns the url to the permissions page of the specified item.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public string GetItemPermissionsURL(SPListItem item)
{
return webURL + "/_layouts/User.aspx?obj={" + library.ID + "}," + item.ID + ",LISTITEM&List={" + library.ID + "}";
}
public string GetNewformURLOfLibrary(string rootFolder, bool multiUpload)
{
return libraryNewFormURL + "/" + rootFolder + "&IsDlg=1" + (multiUpload ? "&MultipleUpload=1" : "");
}
}